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

Posted 高晓牛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于ios 图片截取功能 图片拼接功能的主要内容,如果未能解决你的问题,请参考以下文章

opencv怎么截取视频图片

iOS开发类似于呱呱卡效果,手指划过的区域形成画笔。适用于取出部分图片(截图),如截取出图片中带文字的区域部分。

如何利用ffmpeg将一小段视频截取成图片

iOS 视频截取

Android实现截屏和截长图功能的各种方法

图片截取