user2889249
user2889249

Reputation: 909

How to set an image into an UIImageView without any quality loss and getting the average colour of the image

I want to do something like this. Im selecting maximum 8 images from my phone photo gallery. And I set it into several image views. Those image views are same size. But my images are different sizes.So some images looks so ugly because of stretching.

So How can I set the image inside a UIImageView without any quality loss.

Also How can I set the UIImageview bckground colour as the average colour of the current image which inside of that UIImageView

Upvotes: 0

Views: 886

Answers (2)

Ty Lertwichaiworawit
Ty Lertwichaiworawit

Reputation: 2950

Use UIViewContentModeScaleAspectFit like this...

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myimage.png"]];
    [imageView setContentMode:UIViewContentModeScaleAspectFit];
    [self.view addSubview:imageView];

And your image name should be [email protected] if possible to get the original quality

UPDATE

If you are getting image from the image picker, use this..

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage * pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
    [imageView setContentMode:UIViewContentModeScaleAspectFit];
    [imageView setImage:pickedImage];
    [self.view addSubview:imageView];
}

imageView frame 320x200 pixels with UIViewContentModeScaleAspectFill

enter image description here

imageView frame 320x200 pixels with UIViewContentModeScaleAspectFilt

enter image description here

Upvotes: 1

Anthony Geranio
Anthony Geranio

Reputation: 56

You can use the contentMode property to scale your image accordingly.

For example,

image.contentMode = UIViewContentModeScaleToFill;

This will scale your image to the size of your UIImageView frame.

If you're loading images in from a server, you will have to load the data according to UIImageJPEGRepresentation. That takes two parameters, the image and quality.

For example,

NSData *data = UIImageJPEGRepresentation(image, 1.0);

Where 1.0 is the highest quality :)

Good luck!

Upvotes: 2

Related Questions