4

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 image ?

dplante
  • 2,445
  • 3
  • 21
  • 27
Durga
  • 933
  • 1
  • 9
  • 12

5 Answers5

1

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.

1

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 ;
}
iCoder4777
  • 1,682
  • 1
  • 14
  • 35
1

[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..

Community
  • 1
  • 1
alex-i
  • 5,406
  • 2
  • 36
  • 56
0

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

Banker Mittal
  • 1,918
  • 14
  • 26
0

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;
}
Cyprian
  • 9,423
  • 4
  • 39
  • 73
  • This works, but in iOS5.0.1 its causing a memory leak. Is there an alternative approach? http://stackoverflow.com/questions/8236837/memory-leak-drawinrect-on-ios5-0-1 – David Nov 23 '11 at 14:22