当前位置:   article > 正文

UIImage 使用UIGraphicsBeginImageContext做一些简单操作

uigraphicsbeginimagecontext uiimage

UIImage 绘制


UIGraphicsBeginImageContext

步骤:

  1. UIGraphicsBeginImageContext(CGSize size) 或者 UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)
    • 准备绘图环境
  • CGContextRef __nullable UIGraphicsGetCurrentContext(void)
    • 获取绘图的CGContextRef
  • 开始绘图
  • UIImage* UIGraphicsGetImageFromCurrentImageContext(void);
    • 获取当前绘制的图形
  • void UIGraphicsEndImageContext(void);
    • 关闭绘图环境
简单应用:

等比缩放 将图片改为自定义大小

  1. - (UIImage *) scaleImage:(UIImage *)image toScale:(float)scaleSize {
  2. UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize);
  3. [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
  4. UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  5. UIGraphicsEndImageContext();
  6. return scaledImage;
  7. }

将UIView转为UIImage

  1. -(UIImage*) captureView:(UIView *)theView {
  2. CGRect rect = theView.frame;
  3. UIGraphicsBeginImageContext(rect.size);
  4. CGContextRef context = UIGraphicsGetCurrentContext();
  5. [theView.layer renderInContext:context];
  6. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  7. UIGraphicsEndImageContext();
  8. return img;
  9. }

** renderInContext 只会绘制在左上角 **

** 若要将UIView绘制在指定地方可以平移context **
如:

  1. CGContextTranslateCTM(context, 50, 50);
  2. [theView.layer renderInContext:context];
  3. CGContextTranslateCTM(context, -50, -50);

根据给定得图片,从其指定区域截取一张新得图片

  1. -(UIImage *)getImageFromImage{
  2. //大图bigImage
  3. //定义myImageRect,截图的区域
  4. CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);
  5. UIImage* bigImage= [UIImage imageNamed:@"k00030.jpg"];
  6. CGImageRef imageRef = bigImage.CGImage;
  7. CGImageRef subImageRef =CGImageCreateWithImageInRect(imageRef, myImageRect);
  8. CGSize size;
  9. size.width = 57.0;
  10. size.height = 57.0;
  11. UIGraphicsBeginImageContext(size);
  12. CGContextRef context = UIGraphicsGetCurrentContext();
  13. CGContextDrawImage(context, myImageRect, subImageRef);
  14. UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
  15. UIGraphicsEndImageContext();
  16. return smallImage;
  17. }

合并两张图片

  1. -(UIImage *)addImage:(UIImage *)image1 toImage:(UIImage*)image2 {
  2. UIGraphicsBeginImageContext(image1.size);
  3. // Draw image1
  4. [image1 drawInRect:CGRectMake(0, 0, image1.size.width,image1.size.height)];
  5. // Draw image2
  6. [image2 drawInRect:CGRectMake(0, 0, image2.size.width,image2.size.height)];
  7. UIImage *resultingImage =UIGraphicsGetImageFromCurrentImageContext();
  8. UIGraphicsEndImageContext();
  9. return resultingImage;
  10. }

截取当前屏幕

  1. CGSize windowSize = behandView.bounds.size;
  2. UIGraphicsBeginImageContextWithOptions(windowSize, YES, 2.0);
  3. CGContextRef context = UIGraphicsGetCurrentContext();
  4. [behandView.window.layer renderInContext:context];
  5. UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
  6. UIGraphicsEndImageContext();

截取scrollview

  1. -(UIImage *)captureScrollView:(UIScrollView *)scrollView{
  2. UIImage* image = nil;
  3. UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, NO, 2.0f);
  4. scrollView.contentOffset = CGPointZero;
  5. scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
  6. [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
  7. image = UIGraphicsGetImageFromCurrentImageContext();
  8. UIGraphicsEndImageContext();
  9. return image;
  10. }

转载于:https://www.cnblogs.com/sunyanyan/p/5212601.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/509948
推荐阅读
相关标签
  

闽ICP备14008679号