CGImageCreateWithImageInRect 切断
CGImageCreateWithImageInRect cutting off
我在 Swift 中使用 CGImageCreateWithImageInRect 在触摸位置创建部分图像的副本。
我的代码运行良好,除非用户触摸屏幕边缘并且矩形大小落在视图框架之外。它不是返回一个正方形,而是 returns 一个矩形(我假设),用于切断视图外的数量。形状的变化会偏离复制图像的位置,因此它不会与下方图像对齐。
如何保持正方形 shape/size 并忽略边界,使我的图像对齐?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!.locationInView(mainImageView?.superview)
UIGraphicsBeginImageContext(self.view.bounds.size)
UIApplication.sharedApplication().keyWindow!.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let crop = CGRectMake(touch.x - CGFloat(brushWidth) / 2, touch.y - CGFloat(brushWidth) / 2, CGFloat(brushWidth), CGFloat(brushWidth))
let imageRef = CGImageCreateWithImageInRect(screenshot.CGImage, crop)
if imageRef != nil {
let newImage: UIImage = UIImage(CGImage: imageRef!, scale: mainImageView.image!.scale, orientation: mainImageView.image!.imageOrientation)
let bgImage = UIImageView(image: processedImage)
bgImage.center = touch
self.view.addSubview(bgImage)
}
}
}
}
这是一个工作示例:
这是一个扭曲图像的例子,因为我触摸了边缘:
CGImageCreateWithImageInRect(screenshot.CGImage, crop) 确实 return 一个裁剪版本。以下是文档中的相关评论:
Quartz performs these tasks to create the subimage:
Adjusts the area specified by the rect parameter to integral bounds by calling the function CGRectIntegral.
Intersects the result with a rectangle whose origin is (0,0) and size is equal to the size of the image specified by the image parameter.
References the pixels within the resulting rectangle, treating the first pixel within the rectangle as the origin of the sub image.
解决此问题的一个简单方法是调整生成的 UIImageView 的位置,使其大小正确。下面是相关的计算:
let screenshotBounds = CGRectMake(0.0, 0.0, screenshot.size.width, screenshot.size.height)
let cropIntegral = CGRectIntegral(crop)
let cropIntersection = CGRectIntersection(cropIntegral, screenshotBounds)
bgImage.center = CGPoint(CGRectGetMidX(cropIntersection), CGRectGetMidY(cropIntersection))
cropIntersection 是我们提取图像的边界矩形(遵循文档中给出的前两个步骤)。因此,我们可以用它把imageView定位到原图的同一个地方。
我在 Swift 中使用 CGImageCreateWithImageInRect 在触摸位置创建部分图像的副本。
我的代码运行良好,除非用户触摸屏幕边缘并且矩形大小落在视图框架之外。它不是返回一个正方形,而是 returns 一个矩形(我假设),用于切断视图外的数量。形状的变化会偏离复制图像的位置,因此它不会与下方图像对齐。
如何保持正方形 shape/size 并忽略边界,使我的图像对齐?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!.locationInView(mainImageView?.superview)
UIGraphicsBeginImageContext(self.view.bounds.size)
UIApplication.sharedApplication().keyWindow!.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let crop = CGRectMake(touch.x - CGFloat(brushWidth) / 2, touch.y - CGFloat(brushWidth) / 2, CGFloat(brushWidth), CGFloat(brushWidth))
let imageRef = CGImageCreateWithImageInRect(screenshot.CGImage, crop)
if imageRef != nil {
let newImage: UIImage = UIImage(CGImage: imageRef!, scale: mainImageView.image!.scale, orientation: mainImageView.image!.imageOrientation)
let bgImage = UIImageView(image: processedImage)
bgImage.center = touch
self.view.addSubview(bgImage)
}
}
}
}
这是一个工作示例:
这是一个扭曲图像的例子,因为我触摸了边缘:
CGImageCreateWithImageInRect(screenshot.CGImage, crop) 确实 return 一个裁剪版本。以下是文档中的相关评论:
Quartz performs these tasks to create the subimage:
Adjusts the area specified by the rect parameter to integral bounds by calling the function CGRectIntegral.
Intersects the result with a rectangle whose origin is (0,0) and size is equal to the size of the image specified by the image parameter.
References the pixels within the resulting rectangle, treating the first pixel within the rectangle as the origin of the sub image.
解决此问题的一个简单方法是调整生成的 UIImageView 的位置,使其大小正确。下面是相关的计算:
let screenshotBounds = CGRectMake(0.0, 0.0, screenshot.size.width, screenshot.size.height)
let cropIntegral = CGRectIntegral(crop)
let cropIntersection = CGRectIntersection(cropIntegral, screenshotBounds)
bgImage.center = CGPoint(CGRectGetMidX(cropIntersection), CGRectGetMidY(cropIntersection))
cropIntersection 是我们提取图像的边界矩形(遵循文档中给出的前两个步骤)。因此,我们可以用它把imageView定位到原图的同一个地方。