Reputation: 909
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
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
imageView frame 320x200 pixels with UIViewContentModeScaleAspectFilt
Upvotes: 1
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