如何从另一个图像的一部分创build一个图像?

如何在iOS Swift中从另一个图像创build图像?

例如,如果我有1000像素(1000像素)的图像(A)。 我如何从图像(A)的中间创build200像素(200像素)的图像(B)。

你可以在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; }