ios – 在UIImage周围添加透明空间

前端之家收集整理的这篇文章主要介绍了ios – 在UIImage周围添加透明空间前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
让我们说,我们有一个600X400的图像,我们想要一个1000X100的新图像,其中包含初始图像在中心和周围的透明空间.如何在代码中实现?

解决方法

您创建一个1000×1000的新图像上下文,在中间绘制您的旧图像,然后从上下文获取新的UI Image.
  1. // Setup a new context with the correct size
  2. CGFloat width = 1000;
  3. CGFloat height = 1000;
  4. UIGraphicsBeginImageContextWithOptions(CGSizeMake(width,height),NO,0.0);
  5. CGContextRef context = UIGraphicsGetCurrentContext();
  6. UIGraphicsPushContext(context);
  7.  
  8. // Now we can draw anything we want into this new context.
  9. CGPoint origin = CGPointMake((width - oldImage.size.width) / 2.0f,(height - oldImage.size.height) / 2.0f);
  10. [oldImage drawAtPoint:origin];
  11.  
  12. // Clean up and get the new image.
  13. UIGraphicsPopContext();
  14. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  15. UIGraphicsEndImageContext();

猜你在找的iOS相关文章