2015-05-09 127 views
3

我們正試圖讓用戶從其專輯(UIImagePickerController)導入圖片,並且我們還要縮放/調整大於8百萬像素(iPhone標準)的圖片的大小。與assetsd的連接被中斷或資產死亡(帶有內存警告)

但每次應用程序後或進口picture.At時間之前Connection to assetsd was interrupted or assetsd diedReceived memory warning警告崩潰時Received memory warning警告彈出時仍然在尋找圖片中UIImagePickerController進口。

特別適用於iPhone 4s這更糟糕的是,請幫助我們優化我們的代碼,以便它沒有警告和崩潰運行在像iPhone 4S或iPad 2的舊設備

讓我們知道,如果我們做錯了什麼使用CoreGraphics縮放/調整圖像大小(因爲這是使用大量內存的地方)。在的UIImagePickerController

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
    { 
     UIImage *selectedImage=[info objectForKey:UIImagePickerControllerOriginalImage]; 
     if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone) 
     { 
      [picker dismissViewControllerAnimated:YES completion:nil]; 
     } 
     else 
     { 
      [popoverController dismissPopoverAnimated:YES]; 
      [self popoverControllerDidDismissPopover:popoverController]; 
     } 

     // COMPRESSING IMAGE 
     NSData *selectedImageData=UIImageJPEGRepresentation(selectedImage, 0.1); 
     UIImage *selectedImageFromData=[UIImage imageWithData:selectedImageData]; 

     // IMAGE ASPECT RATIO 
     CGFloat originalWidth=selectedImageFromData.size.width; 
     CGFloat originalHeight=selectedImageFromData.size.height; 
     CGFloat myWidth=2048; 
     CGFloat myHeight=2048; 
     CGFloat widthRatio=myWidth/originalWidth; 
     CGFloat heightRatio=myHeight/originalHeight; 
     CGFloat dynamicWidth=heightRatio*originalWidth; 
     CGFloat dynamicHeight=widthRatio*originalHeight; 


     //SCALING UIIMAGE MORE THAN 8 MEGAPIXELS 
     if (((selectedImageFromData.size.width>3264) && (selectedImageFromData.size.height>2448)) || ((selectedImageFromData.size.height>3264) && (selectedImageFromData.size.width>2448))) 
     { 



      // DATA FROM UIIMAGE TO CORE GRAPHICS 
      CGImageRef CoreGraphicsImage=selectedImageFromData.CGImage; 
      CGColorSpaceRef colorSpace = CGImageGetColorSpace(CoreGraphicsImage); 
      CGBitmapInfo bitmapInfo=CGImageGetBitmapInfo(CoreGraphicsImage); 
      CGImageGetBitsPerComponent(CoreGraphicsImage); 


      // RESIZING WIDTH OF THE IMAGE 
      if (originalWidth>originalHeight) 
      { 


      CGContextRef context=CGBitmapContextCreate(NULL, myWidth, dynamicHeight, CGImageGetBitsPerComponent(CoreGraphicsImage), CGImageGetBytesPerRow(CoreGraphicsImage), colorSpace, bitmapInfo); 


      CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
      CGContextDrawImage(context, CGRectMake(0, 0, myWidth, dynamicHeight), CoreGraphicsImage); 
      CGImageRef CGscaledImage=CGBitmapContextCreateImage(context); 
       UIImage *CGLastimage = [UIImage imageWithCGImage: CGscaledImage]; 
       NSLog(@"%f",CGLastimage.size.width); 
       NSLog(@"%f",CGLastimage.size.height); 

       VisualEffectImageVIew.image=CGLastimage; 
       BackgroundImageView.image=CGLastimage; 
       ForegroundImageView.image=CGLastimage; 
      } 


      //RESIZING HEIGHT OF THE IMAGE 
      if (originalHeight>originalWidth) 
      { 
       CGContextRef context=CGBitmapContextCreate(NULL, dynamicWidth, myHeight, CGImageGetBitsPerComponent(CoreGraphicsImage), CGImageGetBytesPerRow(CoreGraphicsImage), colorSpace, bitmapInfo); 


       CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
       CGContextDrawImage(context, CGRectMake(0, 0, dynamicWidth, myHeight), CoreGraphicsImage); 
       CGImageRef CGscaledImage=CGBitmapContextCreateImage(context); 
       UIImage *CGLastimage = [UIImage imageWithCGImage: CGscaledImage]; 

       NSLog(@"%f",CGLastimage.size.width); 
       NSLog(@"%f",CGLastimage.size.height); 

       VisualEffectImageVIew.image=CGLastimage; 
       BackgroundImageView.image=CGLastimage; 
       ForegroundImageView.image=CGLastimage; 

      } 


     } 
     else 
     { 
      NSLog(@" HEIGHT %f",selectedImageFromData.size.height); 
      NSLog(@" WIDTH %f",selectedImageFromData.size.width); 

     VisualEffectImageVIew.image=selectedImageFromData; 
     BackgroundImageView.image=selectedImageFromData; 
     ForegroundImageView.image=selectedImageFromData; 
     } 


    } 

內存報表

滾動

http://i.stack.imgur.com/qxx62.png

當縮放/伸縮的UIImage

http://i.stack.imgur.com/ELCA6.png

回答

3

兩個點的時候。

首先,你不釋放你創建的對象 - 你泄漏了大量的內存。無論您是否使用ARC,都必須根據每個CG 創建調用調用CGContextRelease或CGImageRelease。其次,如果你只是想調整大小,使用coregraphics是矯枉過正,請改用UIKit。在下面使用的代碼中,請注意使用@autorelease以確保上下文結束時由ARC清理對象

- (UIImage *)fitImage:(UIImage *)image scaledToFillSize:(CGSize)size { 
    // do not upscale 

    @autoreleasepool { 
     if(image.size.width <= size.width && image.size.height <= size.height) 
      return image; 

     CGFloat scale = MIN(size.width/image.size.width, size.height/image.size.height); 
     CGFloat width = image.size.width * scale; 
     CGFloat height = image.size.height * scale; 

     CGRect imageRect; 
     // center image 
     if(scale != 1.0) { // avoid divide by zero? 
      if(size.width/image.size.width < size.height/image.size.height) { 
       // height needs to be centered 
       imageRect = CGRectMake(0, (size.height-height)/2, width, height); 
      } else { 
       // width needs to be centered 
       imageRect = CGRectMake((size.width-width)/2, 0, width, height); 
      } 
     } 

     UIGraphicsBeginImageContextWithOptions(size, YES, 0); 
     [[UIColor CollageBorderUIColor] setFill]; // otherwise it's ugly black fill 
     UIRectFill(CGRectMake(0, 0, size.width, size.height)); 
     [image drawInRect:imageRect]; 
     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     return newImage; 
    } 
} 
相關問題