我的项目是自动引用计数,但是当我使用CGBitmapContextCreateImage和UIIMage的方法imageWithCGImage时,发生了一些事情。我写了一些测试代码,当代码运行时,内存增加,直到应用程序崩溃。
设备:
【16G,iPad2,视网膜】
编码:
(UIImage*) createImageByImage:(UIImage*)img
{
CGSize imgSize = img.size;
imgSize.width *= [UIScreen mainScreen].scale;
imgSize.height *= [UIScreen mainScreen].scale;
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(nil, imgSize.width, imgSize.height, 8, imgSize.width * (CGColorSpaceGetNumberOfComponents(space) + 1), space, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(space);
CGRect rect;
rect.origin = CGPointMake(0, 0);
rect.size = imgSize;
// here bypass some transformation to the CGContext
CGContextDrawImage(ctx, rect, img.CGImage);
CGImageRef cgImage = CGBitmapContextCreateImage(ctx);
CGContextRelease(ctx);
UIImage* image = [UIImage imageWithCGImage:cgImage scale:[UIScreen mainScreen].scale orientation:UIImageOrientationDown];
CGImageRelease(cgImage);
return image;
}
呼叫者,召集者:
UIImage* baseImg = [UIImage imageNamed:@"corkboard.jpg"]; // any big image,e.g. 1024*768
for (int i = 0; i < 100; i++) {
UIImage* tempImg = [self createImageByImage:baseImg];
tempImg = nil;
}
baseImg = nil;
有没有人可以解释一下?等待您的帮助!!!
另一件事:在代码中,如果我只替换
[UIImage imageWithCGImage:cgImage ....];
和
[UIImage imageWithCGImage:img.CGImage ....];
错误消失了!但是后来功能变了,不能满足原来的需求!