2009-09-02 209 views
0

我在我的應用程序中有一個UITableView,我必須加載一些固定寬度但高度不同的圖像。我使用NSOperationQueue下載圖像異步,並調整大小和裁剪我嘗試使用Jane Sales在此帖子link text提供的解決方案。在顯示在UITableViewCells之前調整大小和裁剪圖像

我做了一個自定義的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的下載操作。

有人能給我一個線索,爲什麼當我嘗試調整大小和裁剪圖像時,爲什麼我收到執行不良訪問錯誤?我嘗試在表格視圖外使用相同的功能。 80%的情況下,但有些情況下,當我收到相同的執行不良訪問錯誤。

謝謝你在前進, 索林

+0

好奇的是,當我在模擬器中測試應用程序時,不會引發exec訪問錯誤。當我在iPhone 3G上測試時,它會在嘗試調整大小和裁剪時引發錯誤。 – 2009-09-02 12:29:46

+0

無法真正幫助您調整大小和裁剪代碼.. – Daniel 2009-09-02 13:39:39

+0

我建議您將圖像保存在應用程序的沙箱中。我認爲在UITableViewCell中動態地裁剪圖像是相當昂貴的。 – runmad 2009-09-02 15:31:29

回答

3

這是我使用的調整和croping的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的子視圖。

希望這可以清楚我在代碼中使用的內容。 謝謝你的幫助。 Sorin

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; 
     } 
+0

我會試試看看它是否有效。謝謝,我會回覆一個反饋。 Sorin – 2009-09-02 21:23:15

+0

它適合你嗎? – 2009-09-11 09:51:09

相關問題