你可能会像我一样,在用renderInContext 这个方法进行截图的时候,总是出现崩溃现象。确无计可施。我当时研究这个问题也研究了好久,最后通过查找各方的资料,法相,出现这个状况的原因是这个方法在调用时候系统内存会急剧增加,从而导致内存泄漏,崩溃时候报的错误是Message from debugger: Terminated due to memory issue
我当时的代码是这样写的
+ (UIImage *) imageWithView:(UIView *)view{
float scale = 1.0f;
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
view.layer.contents = nil;
return img;
}
看似这样写诗文问题的没大事存在着很大的安全隐患,很容易崩溃。唯一解决办法只能是把该方法替换掉。通过查阅大量资料,发现了另外一个方法。
size_t width = _backView.bounds.size.width;
size_t height = _backView.bounds.size.height;
unsigned char *imageBuffer = (unsigned char *)malloc(width*height*4);
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef imageContext =
CGBitmapContextCreate(imageBuffer, width, height, 8, width*4, colourSpace,
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
CGColorSpaceRelease(colourSpace);
[_backView.layer renderInContext:imageContext];
CGImageRef outputImage = CGBitmapContextCreateImage(imageContext);
UIImage * myImage =[UIImage imageWithCGImage:outputImage scale:1.0 orientation:z ZZZZZZZ] ;
callBack(outputImage,array);
CGImageRelease(outputImage);
CGContextRelease(imageContext);
free(imageBuffer);
换成这个方法就可以解决上述问题了。