【问题标题】:UIImage resize aspect fill and cropUIImage 调整宽高比填充和裁剪
【发布时间】:2014-08-25 03:27:14
【问题描述】:

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

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

    imagenUsr = [info objectForKey:UIImagePickerControllerOriginalImage];

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


}

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

【问题讨论】:

    标签: ios objective-c uiimage


    【解决方案1】:

    试试这个:

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

    这将帮助您保持与以前一样的纵横比。

    另一种方法是:

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

    希望这会有所帮助.. :)

    【讨论】:

    • 这太完美了!谢谢!
    • @simon > 欢迎。乐于助人!
    【解决方案2】:

    斯威夫特 3.0 / 4.0

    AspectFill 图片裁剪/调整大小...根据 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
    }
    

    【讨论】:

    • 这做得很好! (除了命名,不是很Swifty):)
    【解决方案3】:

    编辑:不确定我是否错过了这个或它被编辑过,但我没有看到你也想裁剪它。 Rashad 的解决方案应该适合您。

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

    - (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];
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-22
      • 2013-04-01
      • 2010-10-10
      • 2018-11-02
      • 2020-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多