美文网首页ios 截图
iOS开发 截图分享之截图及修改图片大小和分辨率(截满屏,截长图

iOS开发 截图分享之截图及修改图片大小和分辨率(截满屏,截长图

作者: MangoJ | 来源:发表于2018-04-16 15:57 被阅读167次
无论是从用户体验角度还是产品运营角度截图分享功能已经覆盖大部分的APP。本文不介绍如何分享,只介绍几种截屏的方法(原理相同)!希望能帮助有需要的朋友。

不同的产品对功能的需求有所不同,有些APP只要求截取一屏的内容,有些APP需要截取超出一屏的内容。

方法1.截取UIScrollView上所有内容
- (UIImage *)captureScrollView:(UIScrollView *)scrollView {
UIImage *image = nil;
//第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了,设置为[UIScreen mainScreen].scale可以保证转成的图片不失真。
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, NO, 0.0);
{
    CGPoint savedContentOffset = scrollView.contentOffset;
    CGRect savedFrame = scrollView.frame;
    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;
}
方法2.截取屏幕上指定view的内容
- (UIImage *)createImageWithView:(UIView *)view
{

CGSize s = view.bounds.size;
//第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了,设置为[UIScreen mainScreen].scale可以保证转成的图片不失真。
 UIGraphicsBeginImageContextWithOptions(s, NO,[UIScreen mainScreen].scale);

[view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage*image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;
}
方法3.把两张图片合成为一张图片
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
UIGraphicsBeginImageContext(image1.size);

// Draw image1
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];

// Draw image2
[image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return resultingImage;
}
方法4.对图片进行压缩和裁剪
- (NSData *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return UIImageJPEGRepresentation(newImage, 0.8);
}
方法5.截取image指定区域的图片
- (  UIImage  *)getImageByCuttingImage:(  UIImage  *)image ToRect:(  CGRect  )rect{

//  大图  bigImage

//  定义  myImageRect  ,截图的区域

CGRect  toImageRect = rect;

UIImage  * bigImage= image;

CGImageRef  imageRef = bigImage.  CGImage  ;

CGImageRef  subImageRef =  CGImageCreateWithImageInRect  (imageRef, toImageRect);

CGSize  size;

size.  width  = rect.size.width  ;

size.  height  = rect.size.height  ;

UIGraphicsBeginImageContext(size);

CGContextRef  context =  UIGraphicsGetCurrentContext ();

CGContextDrawImage  (context, toImageRect, subImageRef);

UIImage  * smallImage = [ UIImage  imageWithCGImage  :subImageRef];

UIGraphicsEndImageContext();

return  smallImage;

}
每个方法大同小异,可以根据自己的需求修改上面的方法

相关文章

网友评论

    本文标题:iOS开发 截图分享之截图及修改图片大小和分辨率(截满屏,截长图

    本文链接:https://www.haomeiwen.com/subject/bqlekftx.html