2

我正在开发一个应用程序。我正在从服务器获取图像。我想使用图像作为背景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

4 回答 4

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];
于 2013-04-26T09:55:51.663 回答
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);
于 2013-04-26T10:53:45.323 回答
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(); 
于 2013-09-04T13:13:14.737 回答
0

-(UIImage *)ResizeImage :(CGSize) 新尺寸图片:(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;

}

于 2013-04-26T13:23:27.027 回答