在imageview上设置之前更改UIimage的大小

时间:2013-03-06 08:27:03

标签: iphone

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

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

3 个答案:

答案 0 :(得分:2)

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

在downvotes之后编辑

    [_img_view setContentMode:UIViewContentModeScaleAspectFit]

    [_img_view setContentMode:UIViewContentModeScaleAspectFill]

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

不要担心创建其他图片,只需使用contentMode属性并像往常一样设置图片(例如[_img_view setImage:img];

答案 1 :(得分: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:无需传递图像的高度。根据方面拟合,它将在里面计算。

最诚挚的问候。

答案 2 :(得分:1)

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

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

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