简体   繁体   English

给出EXC_BAD_ACCESS的UIImageJPEGRepresentation

[英]UIImageJPEGRepresentation giving EXC_BAD_ACCESS

In my app I am using the camera and photo library to get an UIImage, 在我的应用程序中,我使用相机和照片库来获取UIImage,
this UIImage is then scaled down about 20 times its normal size I then set a NSData object based off the UIImage. 然后将这个UIImage缩小约为其正常大小的20倍,然后根据UIImage设置一个NSData对象。

_regularImage = [self resizeImage:_takenImage width:100 height:100];


-(UIImage *)resizeImage:(UIImage *)anImage width:(int)width height:(int)height
{

    CGImageRef imageRef = [anImage CGImage];

    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);

    if (alphaInfo == kCGImageAlphaNone)
        alphaInfo = kCGImageAlphaNoneSkipLast;


    CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo);

    CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);

    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage *result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return result;      
}

NSData *image1Data = UIImageJPEGRepresentation(_regularImage, 1);

I cant seem to figure anything else out that might cause this 我似乎无法想出任何其他可能导致这种情况的事情

Thank you 谢谢

LittleRy LittleRy

The issue here may be that you are creating your bitmap context or UIImage in the wrong way. 这里的问题可能是您正在以错误的方式创建位图上下文或UIImage。 Try debugging and checking if _regularImage is nil, or if it's invalid. 尝试调试并检查_regularImage是否为nil,或者它是否无效。 For scaling an image, I would suggest using a third party library called ANImageBitmapRep . 为了缩放图像,我建议使用名为ANImageBitmapRep的第三方库。 It's a small set of classes that allows for easy cropping, resizing, rotating, etc of images on the iPhone. 它是一小组类,允许在iPhone上轻松裁剪,调整大小,旋转等图像。 Scaling a UIImage can be done like this: 缩放UIImage可以这样做:

ANImageBitmapRep * irep = [ANImageBitmapRep imageBitmapRepWithImage:myImage];
[irep setSize:BMPointMake(myWidth, myHeight)]; // scale the image
UIImage * theImage = [irep image];
[irep release];
NSData * jpeg = UIImageJPEGRepresentation(theImage, 1);

With this sort of code I doubt that UIImageJPEGRepresentation would be the issue. 有了这种代码,我怀疑UIImageJPEGRepresentation会成为问题。 The ANImageBitmapRep class itself handles the CGContextRef stuff internally, making your job very much easier. ANImageBitmapRep类本身在内部处理CGContextRef内容,使您的工作变得更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM