7

有什么办法可以用尽可能少的行来调整 UIImage 的大小?我不介意比例,我只想将图像分辨率设置为 80x60。就这样

4

4 回答 4

15

这可能有点矫枉过正,但是,您可以简单地获取图像,并以您想要的分辨率创建图形上下文,然后您可以将 tempImage 设置为 UIImageView,并覆盖它。

        UIImage *image = YourImageView.image;
        UIImage *tempImage = nil;
        CGSize targetSize = CGSizeMake(80,60);
        UIGraphicsBeginImageContext(targetSize);

        CGRect thumbnailRect = CGRectMake(0, 0, 0, 0);
        thumbnailRect.origin = CGPointMake(0.0,0.0);
        thumbnailRect.size.width  = targetSize.width;
        thumbnailRect.size.height = targetSize.height;

        [image drawInRect:thumbnailRect];

        tempImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        YourImageView.image = tempImage;
于 2013-03-15T18:27:35.087 回答
1

使用此类(相应地将代码添加到 .h 文件)

#import "UIImage+Resize.h"

@implementation UIImage (Resize)

- (UIImage *)resizedImage:(CGSize)bounds {
   return [self resizedImage:bounds upScale:YES];
}

- (UIImage*)resizedImage:(CGSize)bounds upScale:(BOOL)upScale {
   CGSize originalSize = self.size;
   float xScale = bounds.width / originalSize.width;
   float yScale = bounds.height / originalSize.height;
   float scale = MIN(xScale, yScale);

   if (!upScale) {
      scale = MIN(scale, 1);
   }

   CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale);
   UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale);
   [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
   UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return resultImage;
}
于 2013-03-15T18:29:29.507 回答
1
- (void)resizeImage:(UIImage *)image
{
   CGSize origImageSize = [image size];
   CGRect imageRect = CGRectMake(0, 0, 80, 60);
   float ratio = MAX(newRect.size.width / origImageSize.width,
                  newRect.size.height / origImageSize.height);
   UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);
   UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:newRect
                                                cornerRadius:5.0];
   [path addClip];
   CGRect imageRect;
   imageRect.size.width = ratio * origImageSize.width;
   imageRect.size.height = ratio * origImageSize.height;
   imageRect.origin.x = (newRect.size.width - imageRect.size.width) / 2.0;
   imageRect.origin.y = (newRect.size.height - imageRect.size.height) / 2.0;
   [image drawInRect:imageRect];
   UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
   NSData *data = UIImagePNGRepresentation(smallImage);
   UIGraphicsEndImageContext();
}

不要忘记添加 CoreGraphics 框架。

于 2013-03-15T19:18:14.020 回答
0

我在此页面上找到了以下代码:

- (UIImage*)imageWithImage: (UIImage*) sourceImage scaledToWidth: (float) i_width{

if (sourceImage.size.width>sourceImage.size.height) {
    sourceImage = [[UIImage alloc] initWithCGImage: sourceImage.CGImage
                                             scale: 1.0
                                       orientation: UIImageOrientationRight];
}

float oldWidth = sourceImage.size.width;
float scaleFactor = i_width / oldWidth;
float newHeight = sourceImage.size.height * scaleFactor;
float newWidth = oldWidth * scaleFactor;

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

您所要做的就是为它提供i_width,它会相应地缩放它。

我给它添加了一点扭曲,这样如果图片处于横向模式,它将被旋转为纵向,然后调整大小。如果您希望它是相反的(纵向到横向),请更改:

    if (sourceImage.size.width>sourceImage.size.height) {
    sourceImage = [[UIImage alloc] initWithCGImage: sourceImage.CGImage
                                             scale: 1.0
                                       orientation: UIImageOrientationRight];
}

对此:

    if (sourceImage.size.height>sourceImage.size.width) {
    sourceImage = [[UIImage alloc] initWithCGImage: sourceImage.CGImage
                                             scale: 1.0
                                       orientation: UIImageOrientationRight];
}

注意:我的旋转方法没有考虑图片是指向左还是右。换句话说,如果图像是倒置的横向,我的代码将无法识别。我希望其他人可以对此有所了解:)

于 2015-09-08T12:02:43.283 回答