2013-07-03 59 views
1

我有很多圖像到我的應用程序文檔庫。我想顯示縮略圖文檔目錄圖像的圖像。ALAsset庫+文檔目錄圖像?

我正在使用以下代碼。

UIImage *thumbImage=[UIImage imageWithContentsOfFile:strImagePath]; 
if (thumbImage) { 
    [thubImageView setImage:thumbImage]; 
} 

它爲我工作,但問題是因爲內存警告該應用程序凍結和崩潰。

我已經試過[1]:https://github.com/SlaunchaMan/GCDExample「GCDExample」但我得到了相同的結果。

ALAsset庫用於獲取縮略圖。但我不知道如何使用它與文檔目錄圖像。

和是否有任何其他的方式來顯示,而不內存警告縮略圖。

在此先感謝。

+0

您使用哪種類型的視圖來顯示圖像列表? UICollectionView,UITableView或其他什麼? – Sunama

+0

我使用的UITableView .. –

+0

你應該只加載它們在屏幕上當時實際可見的圖像。另外,您應該考慮將縮略圖保存到磁盤,以便您不必在運行中加載全尺寸圖像和縮小比例。 – Wain

回答

1

類似的問題已回答here

U可以用於創建縮略圖圖像的方法

- (UIImage *)thumbnailImage:(NSInteger)thumbnailSize 
     transparentBorder:(NSUInteger)borderSize 
      cornerRadius:(NSUInteger)cornerRadius 
    interpolationQuality:(CGInterpolationQuality)quality; 

UIImage類別中可用here

希望這有助於。

0

沒有看到更多的代碼,這是很困難的更準確地回答這個問題,但解決這一問題的一般解決方法是懶洋洋地加載你需要爲顯示在表格單元格的圖像,並確保圖像足夠記憶力很小。

所提供的這種使用全局GCD隊列GCDExample一樣。我不會推薦這種方法,使用併發隊列與I/O耦合可能會導致意外的線程爆炸。看到這個優秀的Mike Ash文章進行了非常詳細的分析。在這裏使用串行GCD隊列是一個更明智的方法。

0

使任何大小的恩所有圖像拇指圖片:80×80的臨時目錄在您需要的程序區使用的話記得刪除這些臨時目錄中的與你的任務完成的情況。

這一類的實現代碼將有助於您:

#import "UIImage+Resize.h" 
@implementation UIImage (ResizeCategory) 

-(UIImage*)resizedImageToSize:(CGSize)dstSize 
{ 
    CGImageRef imgRef = self.CGImage; 
    // the below values are regardless of orientation : for UIImages from Camera, width>height (landscape) 
    CGSize srcSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); // not equivalent to self.size (which is dependant on the imageOrientation)! 

    CGFloat scaleRatio = dstSize.width/srcSize.width; 

    UIImageOrientation orient = self.imageOrientation; 
    CGAffineTransform transform = CGAffineTransformIdentity; 
    switch(orient) { 

     case UIImageOrientationUp: //EXIF = 1 
      transform = CGAffineTransformIdentity; 
      break; 

     case UIImageOrientationUpMirrored: //EXIF = 2 
      transform = CGAffineTransformMakeTranslation(srcSize.width, 0.0); 
      transform = CGAffineTransformScale(transform, -1.0, 1.0); 
      break; 

     case UIImageOrientationDown: //EXIF = 3 
      transform = CGAffineTransformMakeTranslation(srcSize.width, srcSize.height); 
      transform = CGAffineTransformRotate(transform, M_PI); 
      break; 

     case UIImageOrientationDownMirrored: //EXIF = 4 
      transform = CGAffineTransformMakeTranslation(0.0, srcSize.height); 
      transform = CGAffineTransformScale(transform, 1.0, -1.0); 
      break; 

     case UIImageOrientationLeftMirrored: //EXIF = 5 
      dstSize = CGSizeMake(dstSize.height, dstSize.width); 
      transform = CGAffineTransformMakeTranslation(srcSize.height, srcSize.width); 
      transform = CGAffineTransformScale(transform, -1.0, 1.0); 
      transform = CGAffineTransformRotate(transform, 3.0 * M_PI_2); 
      break; 

     case UIImageOrientationLeft: //EXIF = 6 
      dstSize = CGSizeMake(dstSize.height, dstSize.width); 
      transform = CGAffineTransformMakeTranslation(0.0, srcSize.width); 
      transform = CGAffineTransformRotate(transform, 3.0 * M_PI_2); 
      break; 

     case UIImageOrientationRightMirrored: //EXIF = 7 
      dstSize = CGSizeMake(dstSize.height, dstSize.width); 
      transform = CGAffineTransformMakeScale(-1.0, 1.0); 
      transform = CGAffineTransformRotate(transform, M_PI_2); 
      break; 

     case UIImageOrientationRight: //EXIF = 8 
      dstSize = CGSizeMake(dstSize.height, dstSize.width); 
      transform = CGAffineTransformMakeTranslation(srcSize.height, 0.0); 
      transform = CGAffineTransformRotate(transform, M_PI_2); 
      break; 

     default: 
      [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"]; 


    } 

    // The actual resize: draw the image on a new context, applying a transform matrix 

    UIGraphicsBeginImageContext(dstSize); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { 
     CGContextScaleCTM(context, -scaleRatio, scaleRatio); 
     CGContextTranslateCTM(context, -srcSize.height, 0); 
    } else { 
     CGContextScaleCTM(context, scaleRatio, -scaleRatio); 
     CGContextTranslateCTM(context, 0, -srcSize.height); 
    } 

    CGContextConcatCTM(context, transform); 

    // we use srcSize (and not dstSize) as the size to specify is in user space (and we use the CTM to apply a scaleRatio) 
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, srcSize.width, srcSize.height), imgRef); 
    UIImage* resizedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return resizedImage;} 

-(UIImage*)resizedImageToFitInSize:(CGSize)boundingSize scaleIfSmaller:(BOOL)scale 
{ 
    // get the image size (independant of imageOrientation) 
    CGImageRef imgRef = self.CGImage; 
    CGSize srcSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); // not equivalent to self.size (which depends on the imageOrientation)! 

    // adjust boundingSize to make it independant on imageOrientation too for farther computations 
    UIImageOrientation orient = self.imageOrientation; 
    switch (orient) { 
     case UIImageOrientationLeft: 
     case UIImageOrientationRight: 
     case UIImageOrientationLeftMirrored: 
     case UIImageOrientationRightMirrored: 
      boundingSize = CGSizeMake(boundingSize.height, boundingSize.width); 
      break; 
     default: 
      // NOP 
      break; 
    } 

    // Compute the target CGRect in order to keep aspect-ratio 
    CGSize dstSize; 

    if (!scale && (srcSize.width < boundingSize.width) && (srcSize.height < boundingSize.height)) { 
     ////NSLog(@"Image is smaller, and we asked not to scale it in this case (scaleIfSmaller:NO)"); 
     dstSize = srcSize; // no resize (we could directly return 'self' here, but we draw the image anyway to take image orientation into account) 
    } else {   
     CGFloat wRatio = boundingSize.width/srcSize.width; 
     CGFloat hRatio = boundingSize.height/srcSize.height; 

     if (wRatio < hRatio) { 
      ////NSLog(@"Width imposed, Height scaled ; ratio = %f",wRatio); 
      dstSize = CGSizeMake(boundingSize.width, srcSize.height * wRatio); 
     } else { 
      ////NSLog(@"Height imposed, Width scaled ; ratio = %f",hRatio); 
      dstSize = CGSizeMake(srcSize.width * hRatio, boundingSize.height); 
     } 
    } 

    return [self resizedImageToSize:dstSize]; 
} 

@end 


內存問題會自動解決。