CGImageRelease:[Not A Type release]:发送给deallocated实例的消息

编程入门 行业动态 更新时间:2024-10-28 08:20:24
本文介绍了CGImageRelease:[Not A Type release]:发送给deallocated实例的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我收到此错误[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实例的消息

本文发布于:2023-11-25 08:46:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629097.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   发送给   消息   Type   CGImageRelease

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!