0

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

我制作了一个自定义 UITableViewCell 类,其中我有一个在排队操作完成以下载图像时调用的方法。该方法被正确调用并显示图像。当我尝试使用 Jane 提出的方法调整图像大小时,出现了问题。当它到达[sourceImage drawInRect:thumbnailRect]; 我收到一个 exec bad access 错误,我不知道为什么。我这样调用方法:

- (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 bad access 错误吗?我尝试在表格视图之外使用相同的功能。80% 的情况下它有效,但有些情况下我收到相同的 exec 错误访问错误。

提前谢谢你,索林

4

2 回答 2

3

这是我用来调整和裁剪 UIImages 的(代码来自 Jane Sales 解决方案

@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 的子视图。

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

于 2009-09-02T21:21:49.370 回答
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;
        }
于 2009-09-02T20:06:50.347 回答