Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIImage像素内存泄漏?_Iphone_Objective C_Memory Leaks - Fatal编程技术网

Iphone UIImage像素内存泄漏?

Iphone UIImage像素内存泄漏?,iphone,objective-c,memory-leaks,Iphone,Objective C,Memory Leaks,我一直在编写一些代码,它获取图像的像素,修改它们,然后返回图像。当我在仪器中运行它时,它说在标有★. 以下代码仅返回原始像素,不做任何修改: CFDataRef CopyImagePixels(CGImageRef inImage) { return CGDataProviderCopyData(CGImageGetDataProvider(inImage)); } - (UIImage *) imagePixels:(UIImage*)imge { CGImageRef img=imge.

我一直在编写一些代码,它获取图像的像素,修改它们,然后返回图像。当我在仪器中运行它时,它说在标有★. 以下代码仅返回原始像素,不做任何修改:

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; }
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的右侧栏中,单击底部中央窗口中泄漏的对象将在侧栏中显示代码中链接的来源


提前感谢

您发布的代码不会泄漏,因此下面是一些场景

  • 泄漏在别处,您对泄漏结果的解释不正确
  • 没有泄漏,您对泄漏结果的解释不正确
  • 泄漏是错误的(我还没有看到这一点,但可能是您的代码让它混淆了)
当你说

当我用仪器运行它时,它说 代码行有漏洞

你什么意思?据我所知,“泄漏”不能也不会告诉您泄漏的具体原因,它会提供泄漏对象的历史记录,您必须决定是过度保留泄漏对象还是释放不足

如果您在对象的分配历史记录(对象的创建)中只有一行,那么您的问题将被释放,但是您发布的代码中没有一个对象具有单行历史记录


也许你可以发布一个截图?

你返回的UIImage是否有可能被泄露?留在某个地方,迷失方向,永不离去?这会导致底层提供者数据泄漏,但你可能认为泄漏会指出泄漏的UIImage,在这种情况下……嗨,我编辑了这篇文章,并添加了更多信息。我认为泄密实际上可能来自于你的观点?此外,泄漏信息也在帖子中。谢谢你的帮助
-(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; }