gpt4 book ai didi

ios - UIImage 调整纵横比填充和裁剪

转载 作者:技术小花猫 更新时间:2023-10-29 11:14:30 25 4
gpt4 key购买 nike

我有这段代码,我拍了一张尺寸为 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 宽高比以填充拍摄图像的顶部和左侧

最佳答案

swift 3.0/4.0

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
}

关于ios - UIImage 调整纵横比填充和裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25478627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com