0

アプリにUITableViewがあり、幅は固定されているが高さが異なる画像を読み込む必要があります。NSOperationQueueを使用して画像を非同期でダウンロードし、サイズ変更とトリミングのために、この投稿リンクテキストでJaneSalesが提供するソリューションを使用しようとしました。

カスタム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%は機能しますが、同じexecbadaccessエラーが発生する場合があります。

よろしくお願いします、ソリン

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は、カスタムTableViewCellのサブビューであるUIImageViewです。

これにより、コードで使用しているものが少し明確になることを願っています。お手伝いありがとう。ソリン

于 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 に答える