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; }