[英]How can I calculate the crop size (rect) of the UIImage with different resolution
[英]IOS Swift UIImage crop to given rect size
我已经尝试将UIImage裁剪为给定的rect,但它的非裁剪常规尺寸为rect大小来自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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.