• ios 图片截取功能 图片拼接功能


    截取整个view:

    -(UIImage*)captureView:(UIView *)theView{
        CGRect rect = theView.frame;
        if ([theView isKindOfClass:[UIScrollView class]]) {
            rect.size = ((UIScrollView *)theView).contentSize;
        }
    
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [theView.layer renderInContext:context];
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
    }

    截取view上某一部分:

    -(UIImage *)cropImage:(UIImage *)image rect:(CGRect)cropRect
    {
     CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
     UIImage *img = [UIImage imageWithCGImage:imageRef]; 
     CGImageRelease(imageRef);
     return img;
    }

    截取scrollerView:

    - (UIImage *)captureScrollView:(UIScrollView *)scrollView{
        UIImage* image = nil;
        UIGraphicsBeginImageContext(scrollView.contentSize);
        {
            CGPoint savedContentOffset = scrollView.contentOffset;
            CGRect savedFrame = scrollView.frame;
            scrollView.contentOffset = CGPointZero;
            scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
    
            [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
            image = UIGraphicsGetImageFromCurrentImageContext();
    
            scrollView.contentOffset = savedContentOffset;
            scrollView.frame = savedFrame;
        }
        UIGraphicsEndImageContext();
    
        if (image != nil) {
            return image;
        }
        return nil;
    }

    多张图片的拼接:

    - (UIImage *)composeWithHeader:(UIImage *)header content:(UIImage *)content footer:(UIImage *)footer{
        CGSize size = CGSizeMake(content.size.width, header.size.height +content.size.height +footer.size.height);
        UIGraphicsBeginImageContext(size);
        [header drawInRect:CGRectMake(0,
                                      0,
                                      header.size.width,
                                      header.size.height)];
        [content drawInRect:CGRectMake(0,
                                       header.size.height,
                                       content.size.width,
                                       content.size.height)];
        [footer drawInRect:CGRectMake(0,
                                      header.size.height+content.size.height,
                                      footer.size.width,
                                      footer.size.height)];
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
  • 相关阅读:
    “神一般存在”的印度理工学院到底有多牛?
    MobaXterm
    VC Debug和Release区别
    Mock 模拟测试简介及 Mockito 使用入门
    JUnit单元测试教程(翻译自Java Code Geeks)
    JUnit4单元测试入门教程
    单元测试利器 JUnit 4
    深入探索 JUnit 4
    JUnit
    Java泛型之类型擦除
  • 原文地址:https://www.cnblogs.com/gaoxiaoniu/p/5315420.html
Copyright © 2020-2023  润新知