剪切图像时iOS收到内存警告

时间:2013-10-18 21:26:02

标签: ios

拍照后我试图剪切图像。这在Non-Retina设备上工作正常。但在Retina设备上收到内存警告。我认为在我评论时切割图像时存在一些内存泄漏,没有问题。

代码:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    CGSize imageSize = image.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat newDimension = 2360;
    CGFloat widthOffset = 20 * [[UIScreen mainScreen] scale];
    CGFloat heightOffset = 135 * [[UIScreen mainScreen] scale];

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(newDimension, newDimension), NO, 0.);
    [image drawAtPoint:CGPointMake(-widthOffset, -heightOffset)
             blendMode:kCGBlendModeCopy
                 alpha:1.];

    productImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [self dismissViewControllerAnimated:NO completion:nil];

    ...
}

拍照后第一次,我得到了记忆警告,但没有坠毁。当我尝试重新打开相机时,应用程序崩溃了。有人可以帮帮我吗?感谢

1 个答案:

答案 0 :(得分:0)

@autoreleasepool {
    ...
    productImage = UIGraphicsGetImageFromCurrentImageContext();
    ...
}

autoreleasepool可能有所帮助。 UIGraphicsGetImageFromCurrentImageContext返回自动释放的UIImage对象,它会保持一段时间,直到退出autoreleasepool块或主线程上的runloop结束。代码的哪一部分应该被autoreleasepool包围,这取决于你的代码。