UIImage调整方面填充和裁剪的大小

时间:2022-07-27 08:57:28

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

我有这个代码,我用这个尺寸的2448x3264拍照,我需要调整大小并调整到左上角的屏幕显示。对不起我的英语谢谢

- (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

我需要调整到320x568宽高比以填充所拍摄图像的顶部和左侧

3 个解决方案

#1


11  

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.. :)

希望这可以帮助.. :)

#2


3  

EDIT: Not sure if I missed this or it was edited, but I didn't see that you wanted to crop it as well. Rashad's solution should work for you.

编辑:不确定我是否错过了它或它被编辑,但我没有看到你想要裁剪它。拉沙德的解决方案应该适合你。

Have you tried creating an image view with the image and adding it to the view?

您是否尝试使用图像创建图像视图并将其添加到视图中?

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    imagenUsr = [info objectForKey:UIImagePickerControllerOriginalImage];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    imageView.image = imagenUsr;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:imageView];
}

#3


1  

Swift 3.0 / 4.0

Swift 3.0 / 4.0

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

AspectFill图像裁剪/调整大小...根据UIImageview框架大小。

    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
}

#1


11  

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.. :)

希望这可以帮助.. :)

#2


3  

EDIT: Not sure if I missed this or it was edited, but I didn't see that you wanted to crop it as well. Rashad's solution should work for you.

编辑:不确定我是否错过了它或它被编辑,但我没有看到你想要裁剪它。拉沙德的解决方案应该适合你。

Have you tried creating an image view with the image and adding it to the view?

您是否尝试使用图像创建图像视图并将其添加到视图中?

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    imagenUsr = [info objectForKey:UIImagePickerControllerOriginalImage];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    imageView.image = imagenUsr;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:imageView];
}

#3


1  

Swift 3.0 / 4.0

Swift 3.0 / 4.0

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

AspectFill图像裁剪/调整大小...根据UIImageview框架大小。

    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
}