如何以编程方式减小图像的内存大小

时间:2013-04-26 09:45:18

标签: iphone ios

我正在开发一个应用程序。我正在从服务器获取图像。我想将图像用作UIView的背景。当我直接将这些图像用作uiview的背景时,UIView加载速度非常慢。因此,在将该图像设置为UIView的背景之前,我想检查图像大小并希望减小图像的大小。所以请告诉我如何查找和更改图像的大小。我使用下面的代码将图像设置为UIView

的背景
 UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
 self.view.backgroundColor = [UIColor colorWithPatternImage:image];

drawrect方法需要更多时间来执行。

4 个答案:

答案 0 :(得分:2)

这是一个函数,您可以将其放入类别或辅助类中,以编程方式调整UIImage的大小。我发现它在网站的某处,但我现在找不到确切的帖子。

请记住,这需要一些时间来处理。理想情况下,如果您希望在将来使用同一图像时获得性能提升,则应该只执行一次。

这就是:

+ (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize withOriginalImage:(UIImage *)origin {

    UIImage *sourceImage = origin;
    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;
        else
            scaleFactor = heightFactor;

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


    // this is actually the interesting part:

    UIGraphicsBeginImageContext(targetSize);

    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    if(newImage == nil) NSLog(@"could not scale image");

    return newImage ;
}

您可以通过以下方式调用它:

UIImage *mySmallImage = [ImageHelper imageByScalingProportionallyToSize:CGSizeMake(128,128) withOriginalImage:(UIImage *)myLargeImage];

答案 1 :(得分:1)

你必须放弃一些图像质量:

NSData *imageData = UIImageJPEGRepresentation(image, 0.3);   //increase compression by varying the 0.3 the normal is 1.0
image = [UIImage imageWithData:imageData];

编辑:要查看大小,您可以使用:

NSLog(@"%d",imageData.length);

答案 2 :(得分:0)

- (UIImage *)ResizeImage:(CGSize)newsize image:(UIImage *)image {

// UIImage * image = [UIImage imageNamed:@"Lion.jpg"];
float imageHeight = image.size.height;
float imageWidth = image.size.width;

float hfactor = imageWidth / newsize.width;
float vfactor = imageHeight / newsize.height;

float factor = fmax(hfactor, vfactor);
// Divide the size by the greater of the vertical or horizontal shrinkage factor
float newWidth = imageWidth / factor;
float newHeight = imageHeight / factor;

UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight));
[image drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;

}

答案 3 :(得分:0)

我用下面的代码解决了这个问题。希望它可以帮到你:

CGFloat ratio;  

if (img.size.width > 500 || img.size.height > 500) //Checking for image is higher then 500 or not
{
    if (img.size.width > img.size.height)
    {
        ratio = 500 / img.size.width;

    }
    else
    {
        ratio = 500 / img.size.height;

    }

    CGSize imageSize = CGSizeMake(img.size.width * ratio,img.size.height * ratio);
    UIGraphicsBeginImageContext(imageSize);
    CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height);

    [img drawInRect:imageRect];
    img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();