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,2.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,2.0);需要特殊说明一下

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

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

  第三个是图片的质量:一般写最2.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 }

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

 

posted @ 2015-04-24 17:05  akforsure  阅读(1231)  评论(0编辑  收藏  举报