好吧,我有一个难以追踪内存泄漏的世界.运行此脚本时,我没有看到任何内存泄漏,但我的objectalloc正在攀爬.Instruments指向CGBitmapContextCreateImage> create_bitmap_data_provider> malloc,这占我的objectalloc的60%.
使用NSTimer多次调用此代码.
//GET IMAGE FROM RESOURCE DIR
NSString * fileLocation = [[NSBundle mainBundle] pathForResource:imgMain ofType:@"jpg"];
NSData * imageData = [NSData dataWithContentsOfFile:fileLocation];
UIImage * blurMe = [UIImage imageWithData:imageData];
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
UIImage * scaledImage = [blurMe _imageScaledToSize:CGSizeMake(blurMe.size.width / dblBlurLevel, blurMe.size.width / dblBlurLevel) interpolationQuality:3.0];
UIImage * labelImage = [scaledImage _imageScaledToSize:blurMe.size interpolationQuality:3.0];
UIImage * imageCopy = [[UIImage alloc] initWithCGImage:labelImage.CGImage];
[pool drain]; // deallocates scaledImage and labelImage
imgView.image = imageCopy;
[imageCopy release];
Run Code Online (Sandbox Code Playgroud)
下面是模糊功能.我相信objectalloc问题位于此处.也许我只需要一双新鲜的眼睛.如果有人能想到这一点会很棒.对不起它有点长......我会试着缩短它.
@implementation UIImage(Blur)
- (UIImage *)blurredCopy:(int)pixelRadius …Run Code Online (Sandbox Code Playgroud) 我已经在这几个小时了,甚至不知道如何调试这个错误.也许有一位SO专家知道发生了什么.
- (void) prepareSubset {
CGSize size = [image size];
float scale = fminf(1.0f, fmaxf(SUBSET_SIZE / cropRect.size.width, SUBSET_SIZE / cropRect.size.height));
CGPoint offset = CGPointMake(-cropRect.origin.x, -cropRect.origin.y);
subsetWidth = cropRect.size.width * scale;
subsetHeight = cropRect.size.height * scale;
subsetBytesPerRow = ((subsetWidth + 0xf) >> 4) << 4;
subsetData = (unsigned char *)malloc(subsetBytesPerRow * subsetHeight);
CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef ctx = CGBitmapContextCreate(subsetData, subsetWidth, subsetHeight, 8, subsetBytesPerRow, grayColorSpace, kCGImageAlphaNone);
CGColorSpaceRelease(grayColorSpace);
CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
CGContextSetAllowsAntialiasing(ctx, false);
CGContextTranslateCTM(ctx, 0.0, subsetHeight);
CGContextScaleCTM(ctx, 1.0, -1.0);
UIGraphicsPushContext(ctx);
CGRect rect = …Run Code Online (Sandbox Code Playgroud) 我不确定我是否理解如何释放位图上下文.
我正在做以下事情:
CGContextRef context = CGBitmapContextCreate(nil, size.width, size.height, 8, 0, CGColorSpaceCreateDeviceRGB(), kCGBitmapAlphaInfoMask);
.
. // (All straightforward code)
.
CGContextRelease(context);
Run Code Online (Sandbox Code Playgroud)
Xcode Analyze仍然在CGBitmapContextCreate行上给我一个"潜在的内存泄漏".
我究竟做错了什么?
我正在寻找一种方法来优化图像,将其颜色从 32 位转换为 16 位,而不仅仅是调整其大小。这就是我正在做的:
- (UIImage *)optimizeImage:(UIImage *)image
{
float newWidth = image.size.width;
float newHeight = image.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, newWidth, newHeight, 5, newWidth * 4,
colorSpace, kCGImageAlphaNone | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGInterpolationQuality quality = kCGInterpolationHigh;
CGContextSetInterpolationQuality(context, quality);
CGImageRef srcImage = CGImageRetain(image.CGImage);
CGContextDrawImage(context, CGRectMake(0, 0, newWidth, newHeight),
srcImage);
CGImageRelease(srcImage);
CGImageRef dst = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *result = [UIImage imageWithCGImage:dst];
CGImageRelease(dst);
UIGraphicsEndImageContext();
return result;
}
Run Code Online (Sandbox Code Playgroud)
这段代码的问题是,当我运行它时,我收到以下错误:
CGBitmapContextCreate:不支持的参数组合:5 个整数位/组件;16 位/像素;三分量色彩空间;kCGImageAlphaNone; 10392 字节/行。
所以我的问题是:支持的组合是什么CGBitmapContextCreate?这种情况下参数应该选择什么bitmapInfo?请建议。
我写了这段代码:
bitmapData = calloc(1, bitmapByteCount );
context = CGBitmapContextCreate (bitmapData,
pixelsWide,
pixelsHigh,
8,
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaOnly);
Run Code Online (Sandbox Code Playgroud)
当我这样做时,CGBitmapContext是复制我的bitmapData,所以在这些行后我应该写
free(bitmapData);
Run Code Online (Sandbox Code Playgroud) memory-leaks ×3
ios ×2
cgcontext ×1
cgimage ×1
ios4 ×1
iphone ×1
objective-c ×1
uiimage ×1
xcode ×1