0

I am using the UIImagePickerController to select images for my app. But I am facing a issue with the aspect ratio of the selected images.The image is showing larger than its original size and the complete image is not shown.I have tried changing UIViewContentMode property to all possible values still no use.Can anyone guide me how to make it work in?

Here is the implementation of UIImagePickerController delegate

- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo {

     self.imageProfile.contentMode = UIViewContentModeScaleAspectFit;
     self.imageProfile.image = image;
     [picker dismissViewControllerAnimated:YES completion:nil];

}

1 Answers1

0

Well as you are using autolayouts, first define an outlet with your image height constraint will called imageHeightConstraint and then modify your code with this one

- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo {

     float aspectRatio = image.size.height/image.size.width
     self.imageProfile.contentMode = UIViewContentModeScaleAspectFit;
     self.imageProfile.image = image;
     [self.imageHeightConstraint setConstant: self.imageProfile.frame.size.width * aspectRatio];
     [picker dismissViewControllerAnimated:YES completion:nil];
}

EDITED

this is my code

#import "ViewController.h"

@interface ViewController () <UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageProfile;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageConstraintHeigth;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (IBAction)selectImage:(id)sender {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo{

    float aspectRatio = image.size.height/image.size.width;
    self.imageProfile.contentMode = UIViewContentModeScaleAspectFit;
    self.imageProfile.image = image;
    //this if you need adjust heigth according to width
    [self.imageConstraintHeigth setConstant: self.imageProfile.frame.size.width * aspectRatio];

    [picker dismissViewControllerAnimated:YES completion:nil];
}



@end

and this are my constraints enter image description here

I hope this help, for me works just fine

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
  • @ZeenathYousuff Do you have fixed constraints for width and height?, if don't you must add those because the UIImageView class have intrinsic content size and is growing according the image content – Reinier Melian Jun 07 '17 at 12:03
  • i have fixed constraints for both width and height – Zeenath Yousuff Jun 07 '17 at 12:08
  • @ZeenathYousuff I will make my own example, and post my results – Reinier Melian Jun 07 '17 at 12:08
  • @ZeenathYousuff I will make my own example, and post my results, you need your image showing in a fixed width and height don't you? – Reinier Melian Jun 07 '17 at 12:09
  • @ZeenathYousuff my answer was edited, i you need further aclarations you must wait y have to leave now, but latter i will check this answer again – Reinier Melian Jun 07 '17 at 12:30
  • This is what even i have done but i don't know why it is not working for me.Pease post the sample you have done so can check it. – Zeenath Yousuff Jun 07 '17 at 12:34
  • @ZeenathYousuff check this is my repo https://github.com/rmelian2014/SOImagePickerExampleOBj – Reinier Melian Jun 07 '17 at 17:39
  • Yeah I checked the sample you have posted and i can see the same result there as well . Actually i am checking in ipad where image shows a very small section of the original image which is enlarged and distorted. – Zeenath Yousuff Jun 08 '17 at 12:05