【发布时间】:2016-01-29 01:30:31
【问题描述】:
如何在 iOS swift 中从另一个图像创建图像?
例如如果我有 1000 x 1000 像素的图像 (A)。我将如何从图像 (A) 的中间创建 200 像素 x 200 像素的图像 (B)。
【问题讨论】:
-
你可以在stackoverflow.com/a/28513086/1271826中使用
imageByCroppingToBounds。
如何在 iOS swift 中从另一个图像创建图像?
例如如果我有 1000 x 1000 像素的图像 (A)。我将如何从图像 (A) 的中间创建 200 像素 x 200 像素的图像 (B)。
【问题讨论】:
imageByCroppingToBounds。
您可以在 Core Graphics 中轻松做到这一点...
func getSubImage(image:UIImage, subRect:CGRect) -> UIImage {
UIGraphicsBeginImageContextWithOptions(subRect.size, YES, 0);
let ctx = UIGraphicsGetCurrentContext();
let contextOrigin = CGPointMake(-subRect.origin.x, subRect.size.height+subRect.origin.y-image.size.height); // Translates coordinates into Core Graphics space
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -subRect.size.height);
CGContextDrawImage(ctx, CGRectMake(contextOrigin.x, contextOrigin.y, image.size.width, image.size.height), image.CGImage);
let img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
【讨论】: