0

我有图像视图,我在其上显示从库中选择的图像。为了显示高质量的图片,我使用以下方法重新缩放图片。我得到的图像质量是完美的,但我需要根据新创建的图像的大小设置 imageView 框架。但是当我使用 newImage.size.width 它给了我原始图像视图的宽度。请帮我根据显示的图像大小设置图像视图框架。提前致谢

-(UIImage *)scaleImage:(UIImage *)img toRectSize:(CGRect)screenRect
{
    UIGraphicsBeginImageContext(screenRect.size);
    float hfactor = img.size.width / screenRect.size.width;
    float vfactor = img.size.height / screenRect.size.height;

    float factor = MAX(hfactor, vfactor);

    float newWidth = img.size.width / factor;
    float newHeight = img.size.height / factor;

    float leftOffset = (screenRect.size.width - newWidth) / 2;
    float topOffset = (screenRect.size.height - newHeight) / 2;

    CGRect newRect = CGRectMake(leftOffset, topOffset, newWidth, newHeight);
    [img drawInRect:newRect blendMode:kCGBlendModePlusDarker alpha:1];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}
4

3 回答 3

2

试试这个我用来调整图像大小的代码,你也会得到新的框架。比例似乎是固定的,但你可以根据你的要求改变它。

-(UIImage*)ImageResize:(UIImage*)image
{
    if(image==NULL)
    {
        return NULL;
    }
    else
    {
        float actualHeight = image.size.height;
        float actualWidth = image.size.width;
        float imgRatio = actualWidth/actualHeight;
        float maxRatio = 130.0/160.0;

        if(imgRatio!=maxRatio)
        {
            if(imgRatio < maxRatio)
            {
                imgRatio = 160.0 / actualHeight;
                actualWidth = imgRatio * actualWidth;
                actualHeight = 160.0;
            }
            else
            {
                imgRatio = 130.0 / actualWidth;
                actualHeight = imgRatio * actualHeight;
                actualWidth = 130.0;
            }
        }
        CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
        UIGraphicsBeginImageContext(rect.size);
        [image drawInRect:rect];
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
    }
}

以下代码可用于您可以传递的特定图像大小。

 -(UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)wantSize
{
    UIImage * targetImage;
    if (nil == image) {
        targetImage = nil;
    }else{
        CGSize size = image.size;
        CGRect rect;
        if (wantSize.width/wantSize.height > size.width/size.height) {
            rect.size.width = wantSize.height*size.width/size.height;
            rect.size.height = wantSize.height;
            rect.origin.x = (wantSize.width - rect.size.width)/2;
            rect.origin.y = 0;
        } else{
            rect.size.width = wantSize.width;
            rect.size.height = wantSize.width*size.height/size.width;
            rect.origin.x = 0;
            rect.origin.y = (wantSize.height - rect.size.height)/2;
        }
        UIGraphicsBeginImageContext(wantSize);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
        UIRectFill(CGRectMake(0, 0, wantSize.width, wantSize.height));//clear background
        [image drawInRect:rect];
        targetImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    return targetImage;
}
于 2013-09-10T10:22:17.487 回答
1

尝试这个,

resizedImage = [self imageWithImage:originalImage scaledToSize:CGSizeMake(45,45)]; 

self.imageView.image = resizedImage;

- (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize 
{
    UIGraphicsBeginImageContext(newSize);

    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;
}
于 2013-09-10T10:17:25.670 回答
0

你在使用自动布局吗?如果是,您必须更改图像视图的宽度和高度约束而不是框架(您可以像创建其他控件一样创建它们的出口)。

对于自动布局,您可以在 viewDidAppear 方法中更改框架(在 viewWillAppear 或 viewWillLoad 中它可能不起作用。

于 2013-09-10T10:28:42.890 回答