在iPhone sdk中调整大小后获取图像的大小

时间:2013-09-10 10:02:33

标签: iphone ios image-resizing image-scaling image-size

我有图像视图,我正在显示从库中选择的图像。要显示我用于使用以下方法重新缩放图片的高质量图片。我得到的图像质量是完美的,但我需要根据新创建的图像的大小设置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;
}

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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;
}

答案 2 :(得分:0)

您使用的是Autolayout吗? 如果是,则必须更改图像视图的宽度和高度限制而不是框架(您可以使用与其他控件相同的方式创建它们的出口)。

对于自动布局,您可以在viewDidAppear方法中更改帧(在viewWillAppear或viewWillLoad中,它可能无效。