由于iPad 3上的内存警告,应用程序崩溃

我在UIImage上绘制线条,我的图像在UIImageView 。 第一次绘图过程进行得很好,我把新的图像分配给UIImageView但是当我重复这个过程时,它给我的内存警告和应用程序崩溃:

终止响应篮板的终止。

我已经对我的应用程序进行了剖析,CG栅格数据占用了273 MB,总体上还有341 MB的Live Bytes。 还在autorelease池中包装代码,但没有得到成功。 我的代码

 UIGraphicsBeginImageContext(imageView.image.size); [imageView.image drawAtPoint:CGPointMake(0, 0)]; context2=UIGraphicsGetCurrentContext(); for(int i=0; i<kmtaObject.iTotalSize; i++) { kmtaGroup=&kmtaObject.KMTA_GROUP_OBJ[i]; //NSLog(@"Group # = %d",i); for (int j=0; j<kmtaGroup->TotalLines; j++) { lineObject=&kmtaGroup->Line_INFO_OBJ[j]; // NSLog(@"Line # = %d",j); // NSLog(@"*****************"); x0 = lineObject->x0; y0= lineObject->y0; x1= lineObject->x1; y1= lineObject->y1; color= lineObject->Color; lineWidth= lineObject->LinkWidth; lineColor=[self add_colorWithRGBAHexValue:color]; linearColor=lineColor; // Brush width CGContextSetLineWidth(context2, lineWidth); // Line Color CGContextSetStrokeColorWithColor(context2,[linearColor CGColor]); CGContextMoveToPoint(context2, x0, y0); CGContextAddLineToPoint(context2, x1, y1); CGContextStrokePath(context2); } } newImage=UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); imageView.image=newImage; 

所以恰巧我偶然发现了一个类似的问题。 我正在分配一个图像的图像视图,其中的图像本身之前由其他对象保留,正在处理等…结果是图像视图确实泄漏这些图像的每个人。

我使用的解决scheme是在将图像分配给图像视图之前,在CGImage级别上创build图像的副本。 我想必须有一个位图或一些问题。 无论如何,尝试创build一个这样的副本:

 CGImageRef cgCopy = CGImageCreateCopy(originalImage.CGImage); UIImage *copiedImage = [[UIImage alloc] initWithCGImage:cgCopy scale:originalImage.scale orientation:originalImage.imageOrientation]; CGImageRelease(cgCopy); imageView.image = copiedImage; 

在iOS设备中使用的最大分辨率是1024×1024。 我们不能使用超过这个尺寸。 使用2x和3x图像来显示相应的设备尺寸。