iOS 图片截取 截屏

iOS中对图片的截取

转载的话,请标明出自博客园

截取图片的话 需要指定对象。代码不多,我不太会说,贴一下好了

 1 /**
 2  *  大图切小图
 3  *
 4  *  @param BIGimg 大图
 5  *
 6  *  @return 小图
 7  */
 8 -(UIImage *)getImageFromImage :(UIImage*)BIGimg{
 9
10     //大图bigImage
11     //定义myImageRect,截图的区域   相对位置
12     CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);//这个CGRectMake 决定截图位置
13
14     UIImage* bigImage= BIGimg;
15     CGImageRef imageRef = bigImage.CGImage;
16     CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
17
18     CGSize size;
19     size.width = 57.0;                         //这两个量需要设置   也就是新的图片的大小
20     size.height = 57.0;                        //
21     // 截取用的方法
22     UIGraphicsBeginImageContext(size);
23     CGContextRef context = UIGraphicsGetCurrentContext();
24     CGContextDrawImage(context, myImageRect, subImageRef);
25     UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
26     UIGraphicsEndImageContext();
27
28     return smallImage;
29 }

有好多iOSDev 可能还需要下面的两段代码  第一段是截屏的。。

 1 #pragma mark -拿屏幕的截图
 2 /*获取当前截图*/
 3 - (UIImage *)imageFromView:(UIView *)theView  atFrame:(CGRect)rect
 4 {
 5     UIGraphicsBeginImageContextWithOptions(theView.frame.size,YES,1.0);
 6     CGContextRef context = UIGraphicsGetCurrentContext();
 7     CGContextSaveGState(context);
 8     UIRectClip(rect);
 9     [theView.layer renderInContext:context];
10     UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
11     UIGraphicsEndImageContext();
12     return  theImage;
13 }

关于UIGraphicsBeginImageContextWithOptions(theView.frame.size,YES,1.0);需要特殊说明一下

  第一个参数是截取的页面的大小;

  第二个是 是否不透明(看仔细哦,是否不透明)写YES咯;

  第三个是图片的质量:一般写最大值(图片效果最好),也就是1.0

第二段是将方角改成圆的代码(一般用于显示头像)

 1 /**
 2  *  图片改成圆角
 3  *
 4  *  @param image 原图
 5  *  @param inset 圆角的大小
 6  *
 7  *  @return 切好的图
 8  */
 9 -(UIImage*)circleImage:(UIImage*)image withParam:(CGFloat)inset {
10     UIGraphicsBeginImageContext(image.size);
11     CGContextRef context = UIGraphicsGetCurrentContext();
12     CGContextSetLineWidth(context, 2);
13     CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
14     CGRect rect = CGRectMake(inset, inset, image.size.width - inset * 2.0f, image.size.height - inset * 2.0f);
15     CGContextAddEllipseInRect(context, rect);
16     CGContextClip(context);
17
18     [image drawInRect:rect];
19     CGContextAddEllipseInRect(context, rect);
20     CGContextStrokePath(context);
21     UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
22     UIGraphicsEndImageContext();
23     return newimg;
24 }

如果有什么不妥之处 希望大家指出 交流学习

时间: 2024-07-30 21:18:52

iOS 图片截取 截屏的相关文章

iOS中的截屏(屏幕截屏及scrollView或tableView的全部截屏)

iOS中的截屏(屏幕截屏及scrollView或tableView的全部截屏) 2017.03.16 12:18* 字数 52 阅读 563评论 4喜欢 2 1. 截取屏幕尺寸大小的图片并保存至相册 保存至相册只需将方法saveImage中的代码替换即可 UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 0.0); [self.view.layer renderInContext:UIGraphicsGetCur

iOS模块器截屏闪退

最近不知道什么原因,iOS模块器截屏命令点击模拟器就闪退,在此记录下在命令行截屏操作: 第一步:打开对应的模拟器 第二步:模拟器缩放比为100% 第三步:输入以下命令,001.jpg为要保存的文件名 xcrun simctl io booted screenshot 001.jpg

【Android实战】Bitmap图片的截屏、模糊处理、传递、使用

项目中遇到了这样一个需求: 当某个条件满足时就截取当前屏幕.并跳转到另外一个页面,同一时候将这个截屏图片作为下一个页面的背景图片,同一时候背景图片须要模糊处理 接下来就一步一步解决这个问题: 1.截取无状态栏的当前屏幕图片.请參考takeScreenShot方法 2.使图片高斯模糊的方法请參考blurBitmap方法 注意:RenderScript是Android在API 11之后增加的,用于高效的图片处理,包含模糊.混合.矩阵卷积计算等 public class ScreenShotUtil

Android之Bitmap图片的截屏、模糊处理、传递、使用

项目中遇到了这样一个需求: 当某个条件满足时就截取当前屏幕,并跳转到另外一个页面,同时将这个截屏图片作为下一个页面的背景图片,同时背景图片需要模糊处理 接下来就一步一步解决问题: 1.截取无状态栏的当前屏幕图片,请参考takeScreenShot方法 2.使图片高斯模糊的方法请参考blurBitmap方法 注意:RenderScript是Android在API 11之后加入的,用于高效的图片处理,包括模糊.混合.矩阵卷积计算等 public class ScreenShotUtil { // 获

iOS当前屏幕截屏

需求描述: 有两个ViewController 我们记做 A.B ,其中B controller只是显示下半部分: 如下图效果: 实现这种的方案很多,可以用添加View方法,  也可以用UIWindows 来实现. 但是我这边是想用presentViewController 实现,但是A present  B之后,之前的A就会消失,不会和B 覆盖显示,因此就相当了截取A试图之后在present B. 具体看看实现方案吧: 方法一: 这个方法获取的到图片不会失真 1 -(UIImage *)cap

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 = U

iOS中的截屏

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UIGraphicsBeginImageContextWithOptions(self.view.frame.size, YES, 0.0); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage* image = UIGraphicsGetImageFromCurrent

图片合成截屏

-(UIImage *)saveImage:(UIView *)view {     CGRect mainRect = [[UIScreen mainScreen] bounds];         UIGraphicsBeginImageContext(CGSizeMake(320, 200));     CGContextRef context = UIGraphicsGetCurrentContext();     //    [[UIColor blackColor] set];   

项目总结之截屏细节考虑

DionysosLai([email protected]) 2014/12/22 2014项目总算告一段落,这个产品顺顺利利从开始到最后的上市,圆满成功.借着项目总结,回顾自己做的几个游戏,将一些细节问题归纳.第一篇,就以目前正在做的的新游戏<圣诞节>开篇,讲讲游戏截屏问题. 对于截屏,本身技术,并不是很复杂,一般有两种方法,一种是使用Opengl像素取点方式:另一种是使用RenderTexture纹理方法,详细内容,可以参考,之前写的一篇文章:http://blog.csdn.net/di