基本上,我这样做:
- (UIImage *)addPreviewImageToImage:(UIImage *)backImage
{
UIImage *newImage;
NSString *file = [[NSBundle mainBundle] pathForResource:@"sampleImage" ofType:@"png"];
UIImage *frontImage = [UIImage imageWithContentsOfFile:file];
float scale = 1.0f;
CGRect rectBack = CGRectMake(0, 0, scale * backImage.size.width, scale * backImage.size.height);
CGRect rectFront = [self rectForFrontElementOfSize:frontImage.size overElementOfSize:rectBack.size mediaType:kMediaTypeImage];
// Begin context
UIGraphicsBeginImageContextWithOptions(rectBack.size, YES, 0); // opaque = YES
// draw images
@autoreleasepool { // no effect
[backImage drawInRect:rectBack]; // where the crash occurs, backImage is about 4 Mo
}
[frontImage drawInRect:rectFront];
// grab context
newImage = UIGraphicsGetImageFromCurrentImageContext();
// end context
UIGraphicsEndImageContext();
return newImage;
}
这在iPhone 5或更高版本(iOS7)上运行良好,但在drawInRect:call 4S上80%的时间崩溃。 我该如何优化此代码?将backImage写在磁盘上然后通过contentsOfFile获取它会更有效吗?有什么想法吗?
编辑(崩溃详情): 收到内存警告后,Xcode终止应用程序说明:
"由于记忆压力而终止"
通过使用断点,我知道在执行drawInRect时会发生这种情况:backImage(来自摄像头的图片)
崩溃前的rectBack描述:
(CGRect)rectBack = origin =(x = 0,y = 0)size =(width = 2448,height = 3264)
注意: 在UIGraphicsBeginImageContextWithOptions中将opaque参数设置为YES并减少scale参数似乎使其工作,但我失去了质量,我想知道逻辑中是否存在错误