gpt4 book ai didi

IOS Swift UIImage 裁剪到给定的矩形大小

转载 作者:行者123 更新时间:2023-11-28 07:57:41 30 4
gpt4 key购买 nike

我尝试将 UIImage 裁剪为给定的矩形,但它不是裁剪常规大小矩形大小来自 scrollView

引用代码如下:

guard let view = imageCrop else { print("Error"); return }

let normalizedX = view.contentOffset.x / view.contentSize.width
let normalizedY = view.contentOffset.y / view.contentSize.height

let normalizedWidth = view.frame.width / view.contentSize.width
let normalizedHeight = view.frame.height / view.contentSize.height

let cropRect = CGRect(x: normalizedX, y: normalizedY,
width: normalizedWidth, height: normalizedHeight)

cropImage(image: imageCrop.image, cropRect: cropRect)



func cropImage(imageToCrop:UIImage, toRect rect:CGRect) -> UIImage{

let imageRef:CGImage = imageToCrop.cgImage!.cropping(to: rect)!
let cropped:UIImage = UIImage(cgImage:imageRef)
return cropped
}

最佳答案

已解决。

 var cropArea:CGRect{
get{
let factor = imageCrop.image!.size.width/view.frame.width
let scale = 1/imageCrop.zoomScale
let imageFrame = imageCrop.frame
let x = (imageCrop.contentOffset.x + imageCrop.frame.origin.x - imageFrame.origin.x) * scale * factor
let y = (imageCrop.contentOffset.y + imageCrop.frame.origin.y - imageFrame.origin.y) * scale * factor
let width = imageCrop.frame.size.width * scale * factor
let height = imageCrop.frame.size.height * scale * factor
return CGRect(x: x, y: y, width: width, height: height)
}
}

关于IOS Swift UIImage 裁剪到给定的矩形大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47568161/

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