在UITableViewCells中显示之前调整图像大小并裁剪图像 [英] Resize and crop images before displaying in UITableViewCells

查看:140
本文介绍了在UITableViewCells中显示之前调整图像大小并裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个UITableView,我必须加载一些具有固定宽度但不同高度的图像。我使用NSOperationQueue下载图像异步,并且为了调整大小和裁剪我试图在这篇文章中使用Jane Sales提供的解决方案链接文字

i have an UITableView in my app and i have to load some images that have a fixed width but different heights. I download the images async using an NSOperationQueue and for resizing and cropping i tried to use the solution provided by Jane Sales in this post link text.

我制作了一个自定义的UITableViewCell类,其中我有一个在排队操作完成下载图像时调用的方法。正确调用该方法并显示图像。当我尝试使用Jane提出的方法调整图像大小时出现问题。当它到达 [sourceImage drawInRect:thumbnailRect]; 我收到一个exec错误访问错误,我无法弄清楚原因。我调用这样的方法:

i made a custom UITableViewCell class and in it i have a method that is called when the queued operation finishes to download the image. The method is called properly and the images are displayed. When i try to resize the images using the method proposed by Jane the problems appear. When it reaches [sourceImage drawInRect:thumbnailRect]; i receive an exec bad access error and i can't figure out why. I call the method like this:

- (void) setupImage:(UIImage *) anImage{
    UIImage *resized = [anImage imageByScalingAndCroppingForSize:CGSizeMake(64, 59)];
    if(resized == nil)
        resized = [UIImage newImageFromResource:@"thumb2.png"];
    [thumbnailView setImage:resized];
}

setupImage 是NSOperationQueue结束时调用的函数 anImage 的下载操作。

setupImage is the function called when the NSOperationQueue finishes the download action of anImage.

有人能给我一个线索,当我尝试调整大小并裁剪时,为什么我会收到exec错误访问错误图片?我尝试在表视图外使用相同的功能。 80%的案例可以运行,但有些情况下我收到相同的exec错误访问错误。

Could someone give me a clue why i receive the exec bad access error when i try to resize and crop the images? I tried using the same function outside the table view. 80% of the cases it works but there are cases when i receive the same exec bad access error.

提前谢谢你,
Sorin

Thank you in advance, Sorin

推荐答案

这就是我用来调整和裁剪UIImages的代码(代码来自简销售解决方案

this is what i'm using for resizing and croping the UIImages (the code is from Jane Sales solution)

@implementation UIImage (Extras) 
#pragma mark -
#pragma mark Scale and crop image

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
UIImage *sourceImage = self;
UIImage *newImage = nil;        
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

if (CGSizeEqualToSize(imageSize, targetSize) == NO) 
        {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor > heightFactor) 
                scaleFactor = widthFactor; // scale to fit height
        else
                scaleFactor = heightFactor; // scale to fit width
        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;

        // center the image
        if (widthFactor > heightFactor)
                {
                thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
                }
        else 
                if (widthFactor < heightFactor)
                        {
                        thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
                        }
        }       

UIGraphicsBeginImageContext(targetSize); // this will crop

CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width  = scaledWidth;
thumbnailRect.size.height = scaledHeight;

[sourceImage drawInRect:thumbnailRect];

newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil) 
        NSLog(@"could not scale image");

//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}

我像上面所说的那样调用上面的函数:

i call the above function like i said before:

- (void) setupImage:(UIImage *) anImage
{
    UIImage *resized = [anImage imageByScalingAndCroppingForSize:CGSizeMake(64, 59)];
    [thumbnailView setImage:resized];
}

当在表格视图中显示单元格时,图像将被异步下载。 setupImage函数从NSOperation接收下载异步的图像。问题是,就像我上面说的那样,当它到达[sourceImage drawInRect:thumbnailRect];

the images are downloaded async when a cell is displayed in the table view. the setupImage function receives the image from an NSOperation that downloads it async. the problem is, like i said above, when it reaches [sourceImage drawInRect:thumbnailRect];

thumbnailView是一个UIImageView,它是我的自定义TableViewCell的子视图。

thumbnailView is an UIImageView that is a subview of my custom TableViewCell.

希望这能清除我在代码中使用的内容。
感谢您的帮助。索林

hope this clears things a bit about what i'm using in my code. Thank you for the help. Sorin

这篇关于在UITableViewCells中显示之前调整图像大小并裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆