[英]How to crop or get a smaller size UIImage in iPhone without memory leaks?
我正在使用導航控制器,在其中按如下方式推送tableview控制器:
TableView * Controller = [[TableView alloc] initWithStyle:UITableViewStylePlain]; [self.navigationController pushViewController:Animated:NO]; [控制器發布];
在此表視圖中,我使用以下兩種方法顯示圖像:
- (UIImage*) getSmallImage:(UIImage*) img
{
CGSize size = img.size;
CGFloat ratio = 0;
if (size.width < size.height) {
ratio = 36 / size.width;
} else {
ratio = 36 / size.height;
}
CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);
UIGraphicsBeginImageContext(rect.size);
[img drawInRect:rect];
return UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
//create a context to do our clipping in
UIGraphicsBeginImageContext(rect.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//create a rect with the size we want to crop the image to
//the X and Y here are zero so we start at the beginning of our
//newly created context
CGFloat X = (imageToCrop.size.width - rect.size.width)/2;
CGFloat Y = (imageToCrop.size.height - rect.size.height)/2;
CGRect clippedRect = CGRectMake(X, Y, rect.size.width, rect.size.height);
//CGContextClipToRect( currentContext, clippedRect);
//create a rect equivalent to the full size of the image
//offset the rect by the X and Y we want to start the crop
//from in order to cut off anything before them
CGRect drawRect = CGRectMake(0,
0,
imageToCrop.size.width,
imageToCrop.size.height);
CGContextTranslateCTM(currentContext, 0.0, drawRect.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
//draw the image to our clipped context using our offset rect
//CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);
CGImageRef tmp = CGImageCreateWithImageInRect(imageToCrop.CGImage, clippedRect);
//pull the image from our cropped context
UIImage *cropped = [UIImage imageWithCGImage:tmp];//UIGraphicsGetImageFromCurrentImageContext();
CGImageRelease(tmp);
//pop the context to get back to the default
UIGraphicsEndImageContext();
//Note: this is autoreleased*/
return cropped;
}
但是當我彈出Controller時,正在使用的內存不會釋放。 上面用於創建和裁剪圖像的代碼中是否存在任何泄漏。
也有任何有效的方法來處理iPhone中的圖像。 在解決內存問題時,我有很多圖像並且面臨很多挑戰。
提前發送。
我不知道這一點,但這可能是一個問題:
return UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
第二行永遠不會運行,因為第一行返回
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.