我收到此错误[Not A Type release]:在最后一行代码CGImageRelease(imageToSave);上发送到deallocated instance的消息。请解释我为什么以及需要用它来修复它。我正在使用ARC,但我认为这不适用于CG对象。在测试它们有效之后,我已经用建议的答案更新了代码。
I'm getting this error "[Not A Type release]: message sent to deallocated instance" on the last line of code "CGImageRelease(imageToSave);". Please explain why and what I need to use to fix it. I'm using ARC, but I don't think that applies to CG objects. I've updated the code with the suggested answers after testing that they work.
CGImageRef imageToSave; UIImage *uiImageToSave = [[UIImage alloc] init]; if (sender == nil) { imageToSave = [originalImage CGImage]; } else { uiImageToSave = [self addTitleBlock:annotatedImage]; imageToSave = [uiImageToSave CGImage]; } [library writeImageToSavedPhotosAlbum:imageToSave metadata:imageMetadata completionBlock:^(NSURL *assetURL,NSError *error){ [saveAlertView dismissWithClickedButtonIndex:0 animated:YES]; [activityIndicator stopAnimating]; [activityIndicator removeFromSuperview]; if(error == nil) { if (sender != nil) { [self setToolbarItems:viewingToolbarItems animated:YES]; [UIView beginAnimations:@"savePhoto" context:NULL]; [UIView setAnimationTransition:PHOTO_SAVE forView:pictureView cache:YES]; [UIView setAnimationDuration:0.5f]; [UIView setAnimationDelay:0.0f]; [UIView setAnimationPosition:CGPointMake(45, 430)]; [splashScreen setHidden:NO]; [imageView setHidden:YES]; [sampleImageView setHidden:YES]; [colorImageView setHidden:YES]; [UIView commitAnimations]; } else { [saveButton setEnabled:YES]; [cancelButton setEnabled:YES]; } } else { if (sender != nil) { saveAlertView=[[UIAlertView alloc] initWithTitle:nil message:@"Image Save Failed!" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil]; } else { saveAlertView=[[UIAlertView alloc] initWithTitle:nil message:@"Original Image Save Failed!" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil]; } [saveAlertView show]; [saveButton setEnabled:YES]; [cancelButton setEnabled:YES]; } // CGImageRelease(imageToSave); }];推荐答案
甚至比某人0告诉你的更糟糕:
Even worse than what someone0 is telling you:
我会说你在 else块中定义 uiImageToSave ,你用 imageToSave 在else块之外无效 - 所以只要内存没有被覆盖,代码中任何使用imageToSave的工作都是偶然的。
I would say as you defined your uiImageToSave in the else block, the reference you created with imageToSave is not valid outside of the else block - so any use of imageToSave in your code is just working by accident, as long as the memory is not overwritten yet.
如上所述, [UIImage CGImage] 调用只会为图像数据提供参考,它不会复制或保留它 - 所以你可能不会发布它自己,它会在UIImage不再存在时自动释放 - 在你的情况下,你只需要一行参考。
And as said, the [UIImage CGImage] call only gives you a reference to the image data, it does not make a copy or retain it - so you may not release it yourself, it is released automatically when the UIImage ceases to exist - which in your case is just one line after you make the reference.
更新:
调整原始帖子中的代码 - 现在将uiImageToSave定义在正确的位置(就好像有人想知道我的评论现在不适合原始帖子: - )。
The code in the original posting is adjusted - the uiImageToSave is now defined in the right place (just if anyone wonders about my comment which is now not really fitting the original posting anymore :-).
更多推荐
CGImageRelease:[Not A Type release]:发送给deallocated实例的消息
发布评论