我正在尝试获取 PDF 页面的图像并对其进行编辑。一切正常,但内存增长巨大。Profiler 说没有任何内存泄漏。分析器还说,在 UIGraphicsGetCurrentContext() 和 UIGraphicsGetImageFromCurrentImageContext() 分配了 90% 的内存。此代码不在循环中运行,无需使用@autorelease 包装它。
if ((pageRotation == 0) || (pageRotation == 180) ||(pageRotation == -180)) {
UIGraphicsBeginImageContextWithOptions(cropBox.size, NO, PAGE_QUALITY);
}
else {
UIGraphicsBeginImageContextWithOptions(
CGSizeMake(cropBox.size.height, cropBox.size.width), NO, PAGE_QUALITY);
}
CGContextRef imageContext = UIGraphicsGetCurrentContext();
[PDFPageRenderer renderPage:_PDFPageRef inContext:imageContext pagePoint:CGPointMake(0, 0)];
UIImage *pageImage = UIGraphicsGetImageFromCurrentImageContext();
[[NSNotificationCenter defaultCenter] postNotificationName:@"PAGE_IMAGE_FETCHED" object:pageImage];
UIGraphicsEndImageContext();
但是调试显示只有当我开始编辑获取的图像时才会出现内存增长。对于图像编辑,我使用 Leptonica 库。例如:
+(void) testAction:(UIImage *) image{
PIX * pix =[self getPixFromUIImage:image];
pixConvertTo8(pix, FALSE);
pixDestroy(&pix);
}
在 pixConvertTo8 应用程序占用 13MB 之前,之后 - 50MB。显然增长取决于图像大小。转换方法:
+(PIX *) getPixFromUIImage:(UIImage *) image{
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
UInt8 const* pData = (UInt8*)CFDataGetBytePtr(data);
Pix *myPix = (Pix *) malloc(sizeof(Pix));
CGImageRef myCGImage = [image CGImage];
myPix->w = CGImageGetWidth (myCGImage)-1;
myPix->h = CGImageGetHeight (myCGImage);
myPix->d = CGImageGetBitsPerPixel([image CGImage]) ;
myPix->wpl = CGImageGetBytesPerRow (myCGImage)/4 ;
myPix->data = (l_uint32 *)pData;
myPix->colormap = NULL;
myPix->text="text";
CFRelease(data);
return myPix;
}
PS对不起我糟糕的英语。