我一直在研究一些獲取圖像像素的代碼,修改它們然後返回圖像。當我在樂器中運行時,它表示在標有★的代碼行上有泄漏。下面的代碼只是返回沒有任何修改的原始像素:內存泄漏與UIImage像素?
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; }
此外,在右側欄儀器,點擊一個物體在底部中央窗口的泄漏將顯示你在側邊欄裏的李nk來自你的代碼。
在此先感謝
難道你返回的UIImage是越來越莫名其妙地泄露?保留在某處並迷路,永不離開?這將導致底層提供的數據泄露,但你會覺得泄漏會在這種情況下指出,泄漏的UIImage以及... – 2011-04-09 14:08:37