1

有一个尺寸为 504x320 且图像尺寸为 1080x1920 的图像视图,它比图像视图大,我想在不改变图像质量和内容的情况下更改此图像的大小与图像视图相同。我正在使用这种方法并且效果很好,但是这种方法有点裁剪到实际图像

    - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize image:(UIImage*)image
{
    UIImage *sourceImage = image;
    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; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        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;
            }
    }

    UIGraphicsBeginImageContext(targetSize); // this will crop

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

    [sourceImage drawInRect:thumbnailRect];

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

    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    return newImage;
}
4

3 回答 3

2

尝试通过以下方法在调整大小的帮助下加载图像。

- (UIImage *)editImage:(UIImage *)_image screenWidth:(CGFloat)_width {


CGSize imageSize = CGSizeMake(_width, _width);

if (isRetinaDisplay])
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 2.0);
else
    UIGraphicsBeginImageContext(imageSize);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, CGRectMake(0.0, 0.0, _width, _width));

CGRect imageRect = CGRectMake((_width-_image.size.width)/2, (_width-_image.size.height)/2, _image.size.width, _image.size.height);

[_image drawInRect:imageRect];

UIImage * resizedImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return resizedImage;
}

PS:不需要传递图像的高度。根据方面拟合,它将在内部计算。

此致。

于 2013-03-06T08:35:36.113 回答
2

看看 UIImageView 的内容视图模式,我认为 Aspect Fit 是你想要的

投票后编辑

    [_img_view setContentMode:UIViewContentModeScaleAspectFit]

或者

    [_img_view setContentMode:UIViewContentModeScaleAspectFill]

前者可能在顶部/底部或左侧/右侧有空格以保持纵横比后者将没有空格但可能会遮挡图像的顶部/底部或左侧/右侧

不要担心创建另一个图像,只需使用contentMode属性并像往常一样设置图像(即[_img_view setImage:img];

于 2013-03-06T08:29:05.047 回答
1

这就是@EdgeAkpinar 的意思,应该和你的方法一样,只是更短。

UIImageView *imageView      = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourImage.png"]] autorelease];
imageView.contentMode       = UIViewContentModeScaleAspectFit;
imageView.frame             = CGRectMake(0, 0, 504, 320);

您不需要对图像本身进行任何更改。

于 2013-03-06T08:47:49.780 回答