BSXPCMessage received error for message: Connection interrupted

博主在做關於CIImage類時遇到了這個問題;

BSXPCMessage received error for message: Connection interrupted;


具體是在

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info中,使用

UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];得到原圖;

然後CIImage *tempImage = [CIImageimageWithCGImage:image.CGImage];得到CIImage;

這時操作的CIImage就會報錯;博主搜遍了谷歌百度,見外國的開發者有提到內存的問題,便想到了壓縮圖片.果然,壓縮之後便不報錯了.

廢話不多少,直接上代碼!

//按寬度壓縮圖片!

+ (UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{

    UIImage *newImage =nil;

    CGSize imageSize = sourceImage.size;

    CGFloat width = imageSize.width;

    CGFloat height = imageSize.height;

    CGFloat targetWidth = defineWidth;

    CGFloat targetHeight = height / (width / targetWidth);

    CGSize size =CGSizeMake(targetWidth, targetHeight);

    CGFloat scaleFactor =0.0;

    CGFloat scaledWidth = targetWidth;

    CGFloat scaledHeight = targetHeight;

    CGPoint thumbnailPoint =CGPointMake(0.0,0.0);

    if(CGSizeEqualToSize(imageSize, size) ==NO){

        CGFloat widthFactor = targetWidth / width;

        CGFloat heightFactor = targetHeight / height;

        if(widthFactor > heightFactor){

            scaleFactor = widthFactor;

        }

        else{

            scaleFactor = heightFactor;

        }

        scaledWidth = width * scaleFactor;

        scaledHeight = height * scaleFactor;

        if(widthFactor > heightFactor){

            thumbnailPoint.y = (targetHeight - scaledHeight) *0.5;

        }elseif(widthFactor < heightFactor){

            thumbnailPoint.x = (targetWidth - scaledWidth) *0.5;

        }

    }

    UIGraphicsBeginImageContext(size);

    CGRect thumbnailRect =CGRectZero;

    thumbnailRect.origin = thumbnailPoint;

    thumbnailRect.size.width = scaledWidth;

    thumbnailRect.size.height = scaledHeight+1;

    

    [sourceImage drawInRect:thumbnailRect];

    

    newImage = UIGraphicsGetImageFromCurrentImageContext();

    if(newImage ==nil){

        debugLog(@"scale image fail");

    }

    UIGraphicsEndImageContext();

    return newImage;

}

上面是按寬度壓縮圖片的代碼,壓縮率不算高,但是可以用.也可以用其他方法;

UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];//得到原圖

UIImage *imageAfterCompress = [MYToolsimageCompressForWidth:image targetWidth:640.f];//按寬度640壓縮圖片

CIImage *tempImage = [CIImageimageWithCGImage:imageAfterCompress.CGImage];//得到CIImage,此時操作CIImage就不會報錯了

NSArray *features = [self.detectorfeaturesInImage:tempImage];


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章