BSXPCMessage received error for message: Connection interrupted

来源:互联网 发布:js将空数组转换为数字 编辑:程序博客网 时间:2024/05/20 15:40

博主在做关于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:imagetargetWidth:640.f];//按宽度640压缩图片

CIImage *tempImage = [CIImageimageWithCGImage:imageAfterCompress.CGImage];//得到CIImage,此时操作CIImage就不会报错了

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


1 0