iPhone - 多次 CGBitmapContextCreateImage 调用 - ObjectAlloc 攀爬

发布于 2024-08-05 04:05:19 字数 2415 浏览 13 评论 0原文

还有其他人遇到过这个问题吗? ObjectAlloc 因 CGBitmapContextCreateImage 而攀升。苹果的软件没有完全释放objectalloc吗?

我使用 NSTimer 每秒调整图像大小 12 次。在调整图像大小的过程中,我还通过包含 interpolationQuality 添加了类似 Photoshop 的高斯模糊效果。

使用 Instruments 后,它没有显示任何内存泄漏,但我的 objectalloc 继续攀升。它直接指向CGBitmapContextCreateImage
CGBitmapContextCreateImage>> create_bitmap_data_provide> malloc

有人知道解决方案吗?或者甚至可能的想法?

NSTimer 内的调用

NSString * fileLocation = [[NSBundle mainBundle] pathForResource:imgMain ofType:@"jpg"];
NSData * imageData = [NSData dataWithContentsOfFile:fileLocation];
UIImage * blurMe = [UIImage imageWithData:imageData];

CGRect rect = CGRectMake(0, 0, round(blurMe.size.width /dblBlurLevel), round(blurMe.size.width /dblBlurLevel)); 
UIImage * imageShrink = [self resizedImage: blurMe : rect : 3.0];   

CGRect rect2 = CGRectMake(0, 0, blurMe.size.width , blurMe.size.width ); 
UIImage * imageReize = [self resizedImage: imageShrink : rect2 : 3.0];

imgView.image = imageReize;

调整大小函数

-(UIImage *) resizedImage:(UIImage *)inImage : (CGRect)thumbRect : (double)interpolationQuality
{
    CGImageRef                  imageRef = [inImage CGImage];
    CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);

    if (alphaInfo == kCGImageAlphaNone)
        alphaInfo = kCGImageAlphaNoneSkipLast;

    // Build a bitmap context that's the size of the thumbRect
    CGContextRef bitmap = CGBitmapContextCreate(
                                NULL,
                                thumbRect.size.width,
                                thumbRect.size.height,          
                                CGImageGetBitsPerComponent(imageRef),
                                4 * thumbRect.size.width,       
                                CGImageGetColorSpace(imageRef),
                                alphaInfo
                                );

    // Draw into the context, this scales the image
    CGContextSetInterpolationQuality(bitmap, interpolationQuality);
    CGContextDrawImage(bitmap, thumbRect, imageRef);

    // Get an image from the context and a UIImage
    CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
    UIImage*    result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);   // ok if NULL
    CGImageRelease(ref);

    return [result autorelease];
}

Has anyone else come across this problem? ObjectAlloc climbs as a result of the CGBitmapContextCreateImage. Does Apple's software not fully releasing the objectalloc?

I am resizing images 12 times a second with a NSTimer. During resizing of the image I am also adding a photoshop like Gaussian blur effect by including interpolationQuality.

After using Instruments it does not show any memory leaks but my objectalloc just continues to climb. It points directly to CGBitmapContextCreateImage.
CGBitmapContextCreateImage > create_ bitmap_ data_provide > malloc

Anyone know of a solution? or Even possible ideas?

The Call within the NSTimer

NSString * fileLocation = [[NSBundle mainBundle] pathForResource:imgMain ofType:@"jpg"];
NSData * imageData = [NSData dataWithContentsOfFile:fileLocation];
UIImage * blurMe = [UIImage imageWithData:imageData];

CGRect rect = CGRectMake(0, 0, round(blurMe.size.width /dblBlurLevel), round(blurMe.size.width /dblBlurLevel)); 
UIImage * imageShrink = [self resizedImage: blurMe : rect : 3.0];   

CGRect rect2 = CGRectMake(0, 0, blurMe.size.width , blurMe.size.width ); 
UIImage * imageReize = [self resizedImage: imageShrink : rect2 : 3.0];

imgView.image = imageReize;

The Resize Function

-(UIImage *) resizedImage:(UIImage *)inImage : (CGRect)thumbRect : (double)interpolationQuality
{
    CGImageRef                  imageRef = [inImage CGImage];
    CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);

    if (alphaInfo == kCGImageAlphaNone)
        alphaInfo = kCGImageAlphaNoneSkipLast;

    // Build a bitmap context that's the size of the thumbRect
    CGContextRef bitmap = CGBitmapContextCreate(
                                NULL,
                                thumbRect.size.width,
                                thumbRect.size.height,          
                                CGImageGetBitsPerComponent(imageRef),
                                4 * thumbRect.size.width,       
                                CGImageGetColorSpace(imageRef),
                                alphaInfo
                                );

    // Draw into the context, this scales the image
    CGContextSetInterpolationQuality(bitmap, interpolationQuality);
    CGContextDrawImage(bitmap, thumbRect, imageRef);

    // Get an image from the context and a UIImage
    CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
    UIImage*    result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);   // ok if NULL
    CGImageRelease(ref);

    return [result autorelease];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只怪假的太真实 2024-08-12 04:05:19

该代码过度释放结果

也就是说,问题很可能是 UIImage 没有被释放,并且 UIImage 保留在 CGImage 上,而 CGImage 保留在 CGBitmapContextCreate 下分配的内存。

使用仪器查看 UIImage 是否没有被释放,如果是,请尝试调试原因。

That code overreleases result.

That said, it's likely that the issue is that the UIImage is not getting deallocated, and the UIImage is holding onto the CGImage, and the CGImage is holding onto the memory that was allocated under CGBitmapContextCreate.

Use instruments to see if UIImages are not getting deallocated, and if so try to debug why.

裂开嘴轻声笑有多痛 2024-08-12 04:05:19

我编译并运行了你的代码,我没有看到任何泄漏,我的对象分配也没有继续攀升。我每秒运行代码几次,但在 Instruments 中没有看到任何对象增长。我只在模拟器上运行。我还尝试了 kCGInterpolationNone 而不是 3.0,以防出现问题,但仍然没有泄漏。

不知道为什么我没有得到它们而你却得到了。您可能只想在方法中执行此操作:

-(UIImage *) resizedImage:(UIImage *)inImage : (CGRect)thumbRect : (double)interpolationQuality
{
    CGImageRef                  imageRef = [inImage CGImage];
    CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);

    return inImage;
...

为了使该方法变得毫无意义,然后观察对象分配是否继续增长。如果是这样,那么问题就出在其他地方。

I compiled and ran your code as you have it and I don't see any leaks nor does my object alloc keep climbing. I run the code a couple of times a second and don't see any object growth in Instruments. I am only running on the simulator. I also tried a kCGInterpolationNone instead of 3.0 in case that is the problem, and still no leak.

Not sure why I don't get them and you do. You might want to just do this in the method:

-(UIImage *) resizedImage:(UIImage *)inImage : (CGRect)thumbRect : (double)interpolationQuality
{
    CGImageRef                  imageRef = [inImage CGImage];
    CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);

    return inImage;
...

In order to make this method be meaningless and then watch to see if the object alloc continues to grow. If so, then the problem is elsewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文