在UITableViewCells中显示之前调整图像大小并裁剪图像

时间:2009-09-02 12:25:52

标签: iphone resize uiimage crop

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

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

- (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 的下载操作时调用的函数。

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

提前谢谢你, 索林

2 个答案:

答案 0 :(得分:3)

这是我用来调整和裁剪UIImages的代码(代码来自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;
}

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

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

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

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

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

答案 1 :(得分:0)

尝试以下代码。 createImage:image:width:height方法需要一个源CGImageRef以及将返回的最终UIImage的新宽度和高度。当使用UIImage作为源时,可以按如下方式获取相应的CGImageRef:

UIImage *sourceImage;
CGImageRef *sourceRef = [sourceImage CGImage];

        // Draw the image into a pixelsWide x pixelsHigh bitmap and use that bitmap to 
        // create a new UIImage 
        - (UIImage *) createImage: (CGImageRef) image width: (int) pixelWidth height: (int) pixelHeight
        { 
            // Set the size of the output image 
            CGRect aRect = CGRectMake(0.0f, 0.0f, pixelWidth, pixelHeight); 
            // Create a bitmap context to store the new thumbnail 
            CGContextRef context = MyCreateBitmapContext(pixelWidth, pixelHeight); 
            // Clear the context and draw the image into the rectangle 
            CGContextClearRect(context, aRect); 
            CGContextDrawImage(context, aRect, image); 
            // Return a UIImage populated with the new resized image 
            CGImageRef myRef = CGBitmapContextCreateImage (context); 

            UIImage *img = [UIImage imageWithCGImage:myRef];

            free(CGBitmapContextGetData(context)); 
            CGContextRelease(context);
            CGImageRelease(myRef);

            return img; 
        } 



        // MyCreateBitmapContext: Source based on Apple Sample Code
        CGContextRef MyCreateBitmapContext (int pixelsWide,
                                            int pixelsHigh)
        {
            CGContextRef    context = NULL;
            CGColorSpaceRef colorSpace;
            void *          bitmapData;
            int             bitmapByteCount;
            int             bitmapBytesPerRow;

            bitmapBytesPerRow   = (pixelsWide * 4);
            bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

            colorSpace = CGColorSpaceCreateDeviceRGB();
            bitmapData = malloc( bitmapByteCount );
            if (bitmapData == NULL)
            {
                fprintf (stderr, "Memory not allocated!");
                CGColorSpaceRelease( colorSpace );
                return NULL;
            }
            context = CGBitmapContextCreate (bitmapData,
                                             pixelsWide,
                                             pixelsHigh,
                                             8,
                                             bitmapBytesPerRow,
                                             colorSpace,
                                             kCGImageAlphaPremultipliedLast);
            if (context== NULL)
            {
                free (bitmapData);
                CGColorSpaceRelease( colorSpace );
                fprintf (stderr, "Context not created!");
                return NULL;
            }
            CGColorSpaceRelease( colorSpace );

            return context;
        }