【问题标题】:Compress UIImage压缩 UIImage
【发布时间】:2013-12-04 23:36:58
【问题描述】:

我需要帮助调整 UIImage 的大小。

例如:我在 UICollection 视图中显示了很多图像,但这些图像的大小为 2 到 4 MB。我需要压缩或调整这些图像的大小。

我找到了这个:How to compress/resize image on iPhone OS SDK before uploading to a server?,但我不明白如何实现它。

【问题讨论】:

  • 如果您提供一些代码来展示您迄今为止所尝试的内容,将会有所帮助。
  • 如果您在集合视图同时打开许多图像时解决内存使用问题,那么您希望专注于调整大小,而不是压缩(因为压缩会影响持久内存存储,而不是内存用法)。这是我使用的调整大小例程:stackoverflow.com/a/10491692/1271826
  • 接受的答案与标题匹配,但与问题文本不匹配...

标签: ios iphone objective-c uiimage


【解决方案1】:

不太确定是否要调整大小或压缩或两者兼而有之。

下面是压缩的代码:

通过两个简单的步骤使用JPEG Compression

1) 将 UIImage 转换为 NSData

UIImage *rainyImage =[UImage imageNamed:@"rainy.jpg"];
NSData *imgData= UIImageJPEGRepresentation(rainyImage,0.1 /*compressionQuality*/);

这是有损压缩,图像尺寸减小了。

2) 转换回 UIImage;

UIImage *image=[UIImage imageWithData:imgData];

对于缩放,您可以使用 Matteo Gobbi 提供的答案。但缩放可能不是最好的选择。您宁愿通过压缩获得实际图像的缩略图,因为缩放可能会使您的图像在视网膜显示设备上看起来很糟糕。

【讨论】:

    【解决方案2】:

    我写了这个函数来缩放图像:

    - (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize {
        CGSize actSize = image.size;
        float scale = actSize.width/actSize.height;
    
        if (scale < 1) {
            newSize.height = newSize.width/scale;
        } else {
            newSize.width = newSize.height*scale;
        }
    
    
        UIGraphicsBeginImageContext(newSize);
        [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return newImage;
    }
    

    使用简单,例如:

    [self scaleImage:yourUIImage toSize:CGMakeSize(300,300)];
    

    【讨论】:

      【解决方案3】:
      lowResImage = [UIImage imageWithData:UIImageJPEGRepresentation(highResImage, quality)];
      

      【讨论】:

        【解决方案4】:
         -(UIImage *) resizeImage:(UIImage *)orginalImage resizeSize:(CGSize)size
         {
        CGFloat actualHeight = orginalImage.size.height;
        CGFloat actualWidth = orginalImage.size.width;
        
        float oldRatio = actualWidth/actualHeight;
        float newRatio = size.width/size.height;
        if(oldRatio < newRatio)
        {
            oldRatio = size.height/actualHeight;
            actualWidth = oldRatio * actualWidth;
            actualHeight = size.height;
        }
        else
        {
            oldRatio = size.width/actualWidth;
            actualHeight = oldRatio * actualHeight;
            actualWidth = size.width;
        }
        
        CGRect rect = CGRectMake(0.0,0.0,actualWidth,actualHeight);
        UIGraphicsBeginImageContext(rect.size);
        [orginalImage drawInRect:rect];
        orginalImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return orginalImage;
         }
              //this image you can add it to imageview.....  
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-08-24
          • 1970-01-01
          • 1970-01-01
          • 2014-10-23
          • 1970-01-01
          • 2014-05-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多