Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImage resize aspect fill and crop

I have this code, I take a photo with this size 2448x3264 and i need resize and adjust to display of screen at the top left. sorry for my english thanks

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    imagenUsr = [info objectForKey:UIImagePickerControllerOriginalImage];

    NSLog(@"%f",imagenUsr.size.height);   //2448px
    NSLog(@"%f",imagenUsr.size.width);   //3264px


}

I need adjust to 320x568 aspect to fill at the top and left of the image taken

like image 677
Fabio Avatar asked Aug 25 '14 03:08

Fabio


People also ask

What is the difference between aspect fill and aspect fit when displaying an image?

The Fit setting will resize an image to fit within the bounds of its container with no cropping. The Fill setting will expand an image to fill the whole container, which may result in some cropping depending on the dimensions of your container and the image you send into it.

How do I crop in uiImage Swift?

size let xOffset = (sourceSize. width - sideLength) / 2.0 let yOffset = (sourceSize. height - sideLength) / 2.0 // The cropRect is the rect of the image to keep, // in this case centered let cropRect = CGRect( x: xOffset, y: yOffset, width: sideLength, height: sideLength ).


2 Answers

Try this:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    imagenUsr = [info objectForKey:UIImagePickerControllerOriginalImage];
    double width = imagenUsr.size.width;
    double height = imagenUsr.size.height;
    double screenWidth = self.view.frame.size.width;
    double apect = width/height;
    double nHeight = screenWidth/ apect;
    self.imgView.frame = CGRectMake(0, 0, screenWidth, nHeight);
    self.imgView.center = self.view.center;
    self.imgView.image = imagenUsr;
}

This will help you keeping the aspect ratio as before.

Another way would be:

self.imgView.contentMode = UIViewContentModeScaleAspectFit;
self.imgView.image = imagenUsr;

Hope this helps.. :)

like image 78
Rashad Avatar answered Oct 08 '22 22:10

Rashad


Swift 3.0 / 4.0

AspectFill image cropping/Resizing... according to UIImageview Frame Size.

    func ResizeImage(with image: UIImage?, scaledToFill size: CGSize) -> UIImage? {
    let scale: CGFloat = max(size.width / (image?.size.width ?? 0.0), size.height / (image?.size.height ?? 0.0))
    let width: CGFloat = (image?.size.width ?? 0.0) * scale
    let height: CGFloat = (image?.size.height ?? 0.0) * scale
    let imageRect = CGRect(x: (size.width - width) / 2.0, y: (size.height - height) / 2.0, width: width, height: height)
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    image?.draw(in: imageRect)
    let newImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage
}
like image 39
Varinder Singh iPhone Dev Avatar answered Oct 08 '22 21:10

Varinder Singh iPhone Dev