Get the size of an image after resizing in iPhone sdk

孤人 提交于 2019-12-13 05:00:26

问题


I have image view on which i am displaying image selected from library. To display the fine quality picture i used to rescale the picture using below method. The image quality i am getting is perfect but i need to set the imageView frame according to the size of newly created image. but when i use newImage.size.width it is giving me the width of original image view. Please help me to set the image view frame according to displayed image size. Thanks in advance

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

回答1:


Try this code that I used for resizing image and you will get the new frame as well.The ratio seem to be fixed but you can change it as per your requirement.

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

The below code can be used for specific image size that you can pass.

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



回答2:


Try this,

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



回答3:


Are you using Autolayout? If yes you have to change width and height constraints of your image view not the frame (you can create outlets of them in the same way as you do for other controls).

For autolayout you can change frame in viewDidAppear method (in viewWillAppear or viewWillLoad it may not work.



来源:https://stackoverflow.com/questions/18715931/get-the-size-of-an-image-after-resizing-in-iphone-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!