如何在iPhone中裁剪或获得较小尺寸的UIImage而不出现内存泄漏?
我正在使用一个导航控制器,在该控制器中,我按下tableview控制器,如下所示: TableView*控制器=[[TableView alloc]initWithStyle:UITableViewStylePlain]; [self.navigationController pushview控制器:控制器已设置动画:否]; [控制器释放] 在此表视图中,我使用以下两种方法显示图像:如何在iPhone中裁剪或获得较小尺寸的UIImage而不出现内存泄漏?,iphone,Iphone,我正在使用一个导航控制器,在该控制器中,我按下tableview控制器,如下所示: TableView*控制器=[[TableView alloc]initWithStyle:UITableViewStylePlain]; [self.navigationController pushview控制器:控制器已设置动画:否]; [控制器释放] 在此表视图中,我使用以下两种方法显示图像: - (UIImage*) getSmallImage:(UIImage*) img { CGSize s
- (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;
}
-(UIImage*)getSmallImage:(UIImage*)img
{
CGSize size=img.size;
现金流量比率=0;
如果(尺寸宽度<尺寸高度){
比率=36/尺寸.宽度;
}否则{
比率=36/尺寸.高度;
}
CGRect rect=CGRectMake(0.0,0.0,比率*大小.宽度,比率*大小.高度);
UIGraphicsBeginImageContext(矩形大小);
[img drawInRect:rect];
返回UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
}
-(UIImage*)图像通过裁剪:(UIImage*)图像到复制到复制到复制:(CGRect)rect
{
//创建一个上下文来进行剪辑
UIGraphicsBeginImageContext(矩形大小);
CGContextRef currentContext=UIGraphicsGetCurrentContext();
//创建一个矩形,其大小与我们希望裁剪图像的大小相同
//这里的X和Y为零,所以我们从
//新创建的上下文
CGFloat X=(imageToCrop.size.width-rect.size.width)/2;
CGFloat Y=(imageToCrop.size.height-rect.size.height)/2;
CGRect CLIPEDRECT=CGRectMake(X,Y,rect.size.width,rect.size.height);
//cgContextCliptRect(currentContext,clippedRect);
//创建与图像的完整大小相等的矩形
//用要开始裁剪的X和Y偏移矩形
//为了切断他们面前的一切
CGRect drawRect=CGRectMake(0,
0,
imageToCrop.size.width,
图像分辨率、大小、高度);
CGContextTranslateCm(currentContext,0.0,drawRect.size.height);
CGContextScaleCTM(currentContext,1.0,-1.0);
//使用偏移矩形将图像绘制到剪裁的上下文中
//CGContextDrawImage(currentContext、drawRect、imageToCrop.CGImage);
CGImageRef tmp=CGImageCreateWithImageInRect(imageToCrop.CGImage,clippedRect);
//从裁剪的上下文中提取图像
UIImage*裁剪=[UIImage imageWithCGImage:tmp];//UIGraphicsGetImageFromCurrentImageContext();
CGImageRelease(tmp);
//弹出上下文以返回默认值
UIGraphicsSendImageContext();
//注意:这是自动释放的*/
复种;
}
但是当我打开控制器时,正在使用的内存没有释放。
上述用于创建和裁剪图像的代码中是否存在任何漏洞
还有什么有效的方法来处理iPhone中的图像。我有很多图像,在解决记忆问题方面面临重大挑战
我不知道,但这可能是个问题:
return UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
第二行永远不会运行,因为第一行返回