首先,我的应用程序要求用户从collectionview中选择一张照片或者拍摄相机照片,然后导航到裁剪图像编辑器,这样我们的最终图像将根据裁剪区域的位置变成正方形。但是,问题出在那里关于它的几个来源细节。没有UIImagePicker,怎么做?
我也试过拍广场照,没有成功。
我实现了UIPinchGesture imageview,以便用户可以放大或缩小,但图像视图上没有裁剪方块。我必须添加裁剪区域。
这是裁剪UIImage功能:
func croppImageByRect() -> UIImage {
let ratio: CGFloat = 1 // square
let delta: CGFloat
let offSet: CGPoint
//make a new square size, that is the resized imaged width
let newSize = CGSizeMake(size.width, (size.width - size.width / 8))
//figure out if the picture is landscape or portrait, then
//calculate scale factor and offset
if (size.width > size.height) {
delta = (ratio * size.width - ratio * size.height)
offSet = CGPointMake(delta / 2, 0)
} else {
delta = (ratio * size.height - ratio * size.width)
offSet = CGPointMake(0, delta / 2)
}
//make the final clipping rect based on the calculated values
let clipRect = CGRectMake(-offSet.x, -offSet.y, (ratio * size.width) + delta, (ratio * size.height) + delta)
//start a new context, with scale factor 0.0 so retina displays get
//high quality image
UIGraphicsBeginImageContextWithOptions(newSize, true, 0.0)
UIRectClip(clipRect)
drawInRect(clipRect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
答案 0 :(得分:0)
嗯,首先,这一行:
// make a new square size, that is the resized imaged width
let newSize = CGSizeMake(size.width, (size.width - size.width / 8))
由于宽度和高度不同,不会产生方形尺寸。