简体   繁体   中英

Unable To Get UIImage to Match Custom ImagePickerController Preview

I am building an iOS app that is to have a full screen ImagePickerController, with the resulting image that is captured to be the same that is shown in the ImagePickerController View. This is my current relevant code:

To create and transform the ImagePickerController:

    self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;


CGSize screenSize = [[UIScreen mainScreen] bounds].size;

// set the aspect ratio of the camera
float heightRatio = 4.0f / 3.0f;
// calculate the height of the camera based on the screen width
float cameraHeight = floorf(screenSize.width * heightRatio);
// calculate the ratio that the camera height needs to be scaled by
float scale = ceilf((screenSize.height / cameraHeight) * 10.0) / 10.0;

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;


    self.imagePicker.showsCameraControls = NO;
    [self.imagePicker setCameraOverlayView:cameraView];

    // move the controller to the center of the screen
    self.imagePicker.cameraViewTransform = CGAffineTransformMakeTranslation(0, (screenSize.height - cameraHeight) / 2.0);
    // concatenate the scale transform
    self.imagePicker.cameraViewTransform = CGAffineTransformScale(self.imagePicker.cameraViewTransform, scale, scale);
}

Once the image captured, here is the code I am using to redraw the captured image to match the Preview:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
 self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
 self.image = [self croppedImage:self.image];

- (UIImage *) croppedImage: (UIImage *)image{

CGSize screenSize = [[UIScreen mainScreen] bounds].size;    
// set the aspect ratio of the camera
float heightRatio = 4.0f / 3.0f;
// calculate the height of the camera based on the screen width
float cameraHeight = floorf(screenSize.width * heightRatio);
// calculate the ratio that the camera height needs to be scaled by
float scale = ceilf((screenSize.height / cameraHeight) * 10.0) / 10.0;


CGSize originalImageSize = [image size];
CGSize newImageSize = CGSizeMake(floorf(originalImageSize.width / scale)* 3/4, floorf(originalImageSize.height / scale)* 3/4);

CGRect newImageRect = CGRectMake((originalImageSize.width - newImageSize.width)/2.0, (originalImageSize.height - newImageSize.height)/2.0, newImageSize.width, newImageSize.height);

return [image croppedImage:newImageRect];
}

So my problem is that my CroppedImage method is not calculating correctly, as the resulting image seems to be more "zoomed in" than needed. Not really sure what is wrong in the calculation.

Note - this app is to intended to scale properly on all iPhones - Portrait mode only. I am currently testing on iPhone 6.

In case this helps anybody - on my device i was able to fix it by switching

       CGSize newImageSize = CGSizeMake(floorf(originalImageSize.width / scale)* 3/4, floorf(originalImageSize.height / scale)* 3/4);

to

       CGSize newImageSize = CGSizeMake(floorf(originalImageSize.width / scale), floorf(originalImageSize.height / scale)* 4/3);

only the height/scale needed to by be multiplied and by 4/3, not 3/4. I have not tested this on any other devices yet. Just figured this might help anybody running into the same thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM