开发者

How can I make the large image into small with as good quality as the original image?

I am using imagview with size of 80X80 to display large image (1024 X 780).

While placing the large image into the imageview, the image looks squeezed, compressed something not like the quality one.

My question is, how can I make the large image into small with as good quality as the original i开发者_JS百科mage ?


In the image view, please make the imageview.mode as centre not scale to fit or any other. I believe this would put ur any scaled image at finest quality in ur 80x80 dimension.


In this case, you should do proper Scaling of the Image that u want to show in the ImageView

- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize 
{
    UIImage *sourceImage = chosenImage;
    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;
        else
            scaleFactor = heightFactor;

        scaledWidth  = width * scaleFactor*1;
        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);

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

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage ;
}


[imageView setContentMode:UIViewContentModeScaleAspectFill];

You should however consider resizing the image and saving it on the disk if you want to use it more then once. To resize the image:

The simplest way to resize an UIImage?

UIImage resize (Scale proportion)

UIImage: Resize, then Crop

etc..


Going from 1034x780 into 80x80 is virtually impossible to keep the quality, as there isn't enough space to capture all the details.

You can try to scale it:

+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}


you can set image mode [imageView setContentMode:UIViewContentModeScaleAspectFill]; imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ); [imageView setClipsToBounds:YES];

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜