I am trying to resize an image on the basis of value selected on picker by user.
To this aim, I currently use following code:
- (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)targetSize {
CGRect frame;
UIImage * newImage;
newImage = image;
frame = frontImageView.frame;
frame.size.width = targetSize.width;
frame.size.height = targetSize.height;
frontImageView.frame = frame;
// the pixels will be painted to this array
CGImageRef imageRef = [newImage CGImage]; (APP crash at this point)
CGFloat height = targetSize.height;
CGFloat Width = targetSize.width;
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
pixels = (uint32_t *) malloc(targetSize.width * targetSize.height * sizeof(uint32_t));
// clear the pixels so any transparency is preserved
memset(pixels, 0, Width * height * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
alphaInfo = kCGImageA开发者_运维技巧lphaNoneSkipLast;
CGContextRef bitmap = CGBitmapContextCreate(pixels, Width, height, 8, Width * sizeof(uint32_t), colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
CGContextDrawImage(bitmap, CGRectMake(0, 0, Width, height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
CGImageRelease(newImage);
return result;
}
If I resize an image, the first time (by 25 % for instance) there is no crash. But afterwards, a crash occurs with the error "exec_BAD_Access". How can I solve this?
精彩评论