I want to change my UIImageView height/width to match the image i choose from photo album and i found a snippet on here that does that.
-(CGRect)frameForImage:(UIImage *)imgs inImageViewAspectFit:(UIImageView *)imagev
{
float imageRatio = imgs.size.width / imgs.size.height;
float viewRatio = imagev.frame.size.width / imagev.frame.size.height;
if(imageRatio < viewRatio)
{
float scale = imagev.frame.size.height / imgs.size.height;
float width = scale * imgs.size.width;
float topLeftX = (imagev.frame.size.width - width) * 0.5;
NSLog(@"image after aspect fit: width=%f",width);
imagev.frame = CGRectMake(topLeftX, 0, width, imagev.frame.size.height);
return CGRectMake(topLeftX, 0, width, imagev.frame.size.height);
}
else
{
float scale = imagev.frame.size.width / imgs.size.width;
float height = scale * imgs.size.height;
float topLeftY = (imagev.frame.size.height - height) * 0.5;
NSLog(@"image after aspect fit: height=%f", height);
imagev.frame = CGRectMake(0, topLeftY, imagev.frame.size.width, height);
return CGRectMake(0, topLeftY, imagev.frame.size.width, height);
}
}
The problem is that it changes the UIImageView height/width when i call it from a button
- (IBAction)changeSize:(id)sender
{
[self frameForImage:image inImageViewAspectFit:self.imageView];
}
But i want it to do that as soon as i choose a image from photo album and this is how i have it set up.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self.imageView setImage:image];
[self frameForImage:image inImageViewAspectFit:self.imageView];
[self dismissViewControllerAnimated:YES completion:NULL];
}