![](/img/trans.png)
[英]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.