让我们说,我们有一个600X400的图像,我们想要一个1000X100的新图像,其中包含初始图像在中心和周围的透明空间.如何在代码中实现?
解决方法
您创建一个1000×1000的新图像上下文,在中间绘制您的旧图像,然后从上下文获取新的UI
Image.
// Setup a new context with the correct size CGFloat width = 1000; CGFloat height = 1000; UIGraphicsBeginImageContextWithOptions(CGSizeMake(width,height),NO,0.0); CGContextRef context = UIGraphicsGetCurrentContext(); UIGraphicsPushContext(context); // Now we can draw anything we want into this new context. CGPoint origin = CGPointMake((width - oldImage.size.width) / 2.0f,(height - oldImage.size.height) / 2.0f); [oldImage drawAtPoint:origin]; // Clean up and get the new image. UIGraphicsPopContext(); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();