为什么这个UIImage无法释放

时间:2013-02-16 07:04:50

标签: ios uiimage didreceivememorywarning

我想将图像保存到sandBox ...当我保存了许多图像时,我的应用程序经常崩溃并给予MemoryWarning .....

这是代码:

-(void)saveCurrentLine:(NSDictionary*)lineInfo
{

UIImage* saveImage=[lineInfo objectForKey:@"saveImage"];
NSString* savePath=[lineInfo objectForKey:@"SPN"];
NSLog(@"The savePath is :%@",savePath);
NSString* docs=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString  *pngPath = [docs stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",noteBookName,savePath]];
NSLog(@"%@",pngPath);
[UIImagePNGRepresentation(saveImage) writeToFile:pngPath atomically:YES];
UIImage* saveJPG=[lineInfo objectForKey:@"saveImage"];
UIImage* saveJIV=[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@IV",pngPath]];
NSString  *pngPathS = [docs stringByAppendingPathComponent:[NSString stringWithFormat:@"%@Scan/%@",noteBookName,savePath]];
[UIImageJPEGRepresentation([self addImage:[self scaleToSize:saveJIV size:CGSizeMake(256, 192)] toImage:[self scaleToSize:saveJPG size:CGSizeMake(256, 192)]], 1.0) writeToFile:pngPathS atomically:NO];
NSLog(@"line save over and [saveJPG count] is %d [saveJIV count] is %d [lineInfo count] is %d",[saveJPG retainCount],[saveJIV retainCount],[lineInfo retainCount]);
}

我发现saveJPG和saveJIV没有释放,我无法释放它们......我怎么能让它们释放????

此功能的所有方法:

 -(void)ChangeCanvasTo:(NSNotification*)CanvasInfo
{
self.layer.opacity=1.0;
savePageName=[NSString stringWithFormat:@"%@",PageName];
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSDictionary *currentLine=[[NSDictionary alloc] initWithObjectsAndKeys:saveImage,@"saveImage",savePageName,@"SPN",nil];
[NSThread detachNewThreadSelector:@selector(saveCurrentLine:) toTarget:self withObject:currentLine];
NSString *pngPath=[[CanvasInfo userInfo] objectForKey:@"PageName"];
PageName=pngPath;
NSLog(@"will change to %@",PageName);
NSString* docs=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
UIImage *resumeCanvas=[[UIImage alloc] initWithContentsOfFile:[docs stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",noteBookName,PageName]]];
drawStep=RELOAD;
curImage=resumeCanvas;
[curImage retain];
[self setNeedsDisplay];
[resumeCanvas release];
[currentLine release];

}

-(void)saveCurrentLine:(NSDictionary*)lineInfo
{

UIImage* saveImage=[lineInfo objectForKey:@"saveImage"];
NSString* savePath=[lineInfo objectForKey:@"SPN"];
NSLog(@"The savePath is :%@",savePath);
NSString* docs=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString  *pngPath = [docs stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",noteBookName,savePath]];
NSLog(@"%@",pngPath);
[UIImagePNGRepresentation(saveImage) writeToFile:pngPath atomically:YES];
UIImage* saveJPG=[lineInfo objectForKey:@"saveImage"];
UIImage* saveJIV=[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@IV",pngPath]];
NSString  *pngPathS = [docs stringByAppendingPathComponent:[NSString stringWithFormat:@"%@Scan/%@",noteBookName,savePath]];
[UIImageJPEGRepresentation([self addImage:[self scaleToSize:saveJIV size:CGSizeMake(256, 192)] toImage:[self scaleToSize:saveJPG size:CGSizeMake(256, 192)]], 1.0) writeToFile:pngPathS atomically:NO];
NSLog(@"line save over and [saveJPG count] is %d [saveJIV count] is %d [lineInfo count] is %d",[saveJPG retainCount],[saveJIV retainCount],[lineInfo retainCount]);
}

1 个答案:

答案 0 :(得分:0)

ARC的工作方式是在理解对象不再使用时自动添加“释放”消息。您可以尝试在yor方法的末尾添加saveJPG = nil; saveJIV=nil;

这样ARC就会意识到它可以释放它们,因为它们已经不再使用了。