UIImagePickerController Image Size

I use UIImagePickerController in my application to take snapshots and I use my own controls, which means the UIImagePickerController property shows the CameraControls property equal to NO, and I have a UIButton inside an overlayView that takes pictures. Now I noticed that the image that I save in the Photo Library actually shows a larger area than shown in the preview view. Has anyone else had the same problem? Any solution to get an image showing just what was in the preview?

+1
iphone uiimagepickercontroller
source share
2 answers

In the preview, I assume that you are talking about the image selection interface (and not the default preview screen that appears after rejecting the image selection interface).

The transformations that you apply to the image selection interface (using cameraViewTransform) do not affect the image. For example, if you are trying to scale the scale (scale and scale), you need to apply the same (transformation) on the image obtained in order to synchronize the image in the image selection interface and the actual saved image.
You will also need to consider the orientation of the image when applying transformations.

+1
source share

after the image object from the picker resizes the image, then crop

I needed the same thing - in my case, select a size that fits after scaling, and then crop each end to fit the rest in width. (I work in landscape, so I may not have noticed any flaws in portrait mode.) Here, my code is part of the UIImage category. The target size in my code is always set to full screen size.

@implementation UIImage (Extras) #pragma mark - #pragma mark Scale and crop image - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize { UIImage *sourceImage = self; UIImage *newImage = nil; CGSize imageSize = sourceImage.size; CGFloat width = imageSize.width; CGFloat height = imageSize.height; CGFloat targetWidth = targetSize.width; CGFloat targetHeight = targetSize.height; CGFloat scaleFactor = 0.0; CGFloat scaledWidth = targetWidth; CGFloat scaledHeight = targetHeight; CGPoint thumbnailPoint = CGPointMake(0.0,0.0); if (CGSizeEqualToSize(imageSize, targetSize) == NO) { CGFloat widthFactor = targetWidth / width; CGFloat heightFactor = targetHeight / height; if (widthFactor > heightFactor) scaleFactor = widthFactor; // scale to fit height else scaleFactor = heightFactor; // scale to fit width scaledWidth = width * scaleFactor; scaledHeight = height * scaleFactor; // center the image if (widthFactor > heightFactor) { thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; } else if (widthFactor < heightFactor) { thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; } } UIGraphicsBeginImageContext(targetSize); // this will crop CGRect thumbnailRect = CGRectZero; thumbnailRect.origin = thumbnailPoint; thumbnailRect.size.width = scaledWidth; thumbnailRect.size.height = scaledHeight; [sourceImage drawInRect:thumbnailRect]; newImage = UIGraphicsGetImageFromCurrentImageContext(); if(newImage == nil) NSLog(@"could not scale image"); //pop the context to get back to the default UIGraphicsEndImageContext(); return newImage; } 
+2
source share

All Articles