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 图片截取功能 图片拼接功能的主要内容,如果未能解决你的问题,请参考以下文章