iphone - UIImage 像素内存泄漏?

标签 iphone objective-c memory-leaks

我一直在编写一些代码,这些代码获取图像的像素,修改它们,然后返回图像。当我在 Instruments 中运行它时,它说标有 ★ 的代码行存在泄漏。以下代码仅返回原始像素,没有任何修改:

CFDataRef CopyImagePixels(CGImageRef inImage) {
return CGDataProviderCopyData(CGImageGetDataProvider(inImage)); }

- (UIImage *) imagePixels:(UIImage*)imge {

CGImageRef img=imge.CGImage;
CFDataRef dataref=CopyImagePixels(img);
UInt8 *data=(UInt8 *)CFDataGetBytePtr(dataref);
int length=CFDataGetLength(dataref);

for (int i = 0; i < length; i+=4)
{
    UInt8 b_pixel = data[i];
    UInt8 g_pixel = data[i+1];
    UInt8 r_pixel = data[i+2];

    float outputRed = r_pixel;
    float outputGreen = g_pixel;
    float outputBlue = b_pixel;

    data[i] = outputBlue;
    data[i+1] = outputGreen;
    data[i+2] = outputRed;
}
CFDataRef newData=CFDataCreate(NULL,data,length);★
CGDataProviderRef provider=CGDataProviderCreateWithCFData(newData);★
CGImageRef newImg=CGImageCreate(CGImageGetWidth(img),CGImageGetHeight(img),CGImageGetBitsPerComponent(img),★
                                CGImageGetBitsPerPixel(img),CGImageGetBytesPerRow(img),CGImageGetColorSpace(img),
                                CGImageGetBitmapInfo(img),provider,NULL,true,kCGRenderingIntentDefault);
UIImage*Result = [UIImage imageWithCGImage:newImg];
CGImageRelease(newImg);
CGDataProviderRelease(provider);
CFRelease(newData);
CFRelease(dataref);
return Result; }

我不知道为什么会出现这个。我尝试过寻找答案,但没有一个有效。

谢谢

更新1 泄漏来自此方法:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
ImageView.image = [oimage imagePixels:ImageView.image]; }

oimage 在这里设置:

-(void)viewDidAppear:(BOOL)animated {
mainDelegate = (PhotoAppDelegate *)[[UIApplication sharedApplication]delegate];

CGSize size = mainDelegate.CurrentImage.size; 
UIGraphicsBeginImageContext( size );
[mainDelegate.CurrentImage drawInRect:CGRectMake(0,0,size.width ,size.height )];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();//LEAK
UIGraphicsEndImageContext();

int kMaxResolution = 300;
CGImageRef imgRef = newImage.CGImage;
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGRect bounds = CGRectMake(0, 0, width, height);
if (width > kMaxResolution || height > kMaxResolution) {
    CGFloat ratio = width/height;
    if (ratio > 1) {
        bounds.size.width = kMaxResolution;
        bounds.size.height = bounds.size.width / ratio;
    }
    else {
        bounds.size.height = kMaxResolution;
        bounds.size.width = bounds.size.height * ratio;
    }
}

CGSize newsize = CGSizeMake(bounds.size.width, bounds.size.height);
UIGraphicsBeginImageContext(newsize);
[newImage drawInRect:CGRectMake(0, 0, newsize.width, newsize.height)];

newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
oimage = [newImage retain];
ImageView.image = oimage; }

此外,在 Instruments 的右侧栏中,单击底部中央窗口中的泄漏对象将在侧栏中显示链接来自代码中的位置。

提前致谢

最佳答案

您发布的代码不会泄漏,因此这里有一些场景。

  • 泄漏发生在其他地方,您错误地解释了泄漏的结果。
  • 不存在泄漏,您错误地解释了泄漏结果。
  • Leaks 是错误的(我没见过这个,但也许你的代码有些令人困惑)

当你说

When I run it in Instruments it says there are leaks on the lines of code…

你什么意思?据我所知,Leaks 不能也不会告诉您导致泄漏的特定行,它为您提供泄漏对象的历史记录,您必须决定是否过度保留泄漏对象或释放不足它。

如果对象的分配历史记录(对象的创建)中只有一行,那么您的问题将在于释放,但您发布的代码中的任何对象都不会具有单行历史记录。

也许您可以发布屏幕截图?

关于iphone - UIImage 像素内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5605345/

相关文章:

ios - 从 JSON 调用获取日期时遇到问题

ios - FIRApp.configure() 创建内存泄漏 IOS

java - 关闭 JavaFX 选项卡不会释放内存

javascript - Node : How to free buffers that get allocated outside of the V8 memory heap

iphone - 在 OpenGL ES 中,绘制一个大对象还是绘制多个小对象更快?

iphone - iOS-如何在代码开始之前显示alertView

iphone - 我可以更改uitableview的tableHeaderView的高度吗?

iphone - 呈现 UIPopoverviewcontroller

objective-c - 当 objective-c 中的.h文件为空时该怎么办?

ios - 将圆角半径添加到 UITableViewCell 中的 ImageView 时出现问题