swift3.0截取View生成图片 图片截取成新图片
生活随笔
收集整理的這篇文章主要介紹了
swift3.0截取View生成图片 图片截取成新图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
swift:
//注意/Users/point/Desktop == point是我電腦的用戶名 改成你自己的let img1 = getImage(size: CGSize(width: 200, height: 200),currentView: view)let img2 = getImageFromImage(oldImage: img1, newImageRect: CGRect(x: 100, y: 100, width: 100, height: 100))let imgData = UIImageJPEGRepresentation(img2, 1)NSData(data: imgData!).write(toFile: "/Users/point/Desktop/456.png", atomically: true) //View生成圖片func getImage(size:CGSize , currentView:UIView) -> UIImage {UIGraphicsBeginImageContextWithOptions( size, true, 1.0)currentView.layer.render(in: UIGraphicsGetCurrentContext()!)let image = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return image!;}//圖片中截取圖片func getImageFromImage(oldImage:UIImage,newImageRect:CGRect) ->UIImage {let imageRef = oldImage.cgImage;let subImageRef = imageRef!.cropping(to: newImageRect);return UIImage(cgImage: subImageRef!)}?
贈送OC版本的:
// 從view上截圖- (UIImage *)getImage {UIGraphicsBeginImageContextWithOptions(CGSizeMake(150, 150), NO, 1.0); //NO,YES 控制是否透明[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];UIImage *image = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();// 生成后的imagereturn image;}// 根據給定得圖片,從其指定區域截取一張新得圖片-(UIImage *)getImageFromImage{//大圖bigImage//定義myImageRect,截圖的區域CGRect myImageRect = CGRectMake(70, 10, 150, 150);UIImage* bigImage= [UIImage imageNamed:@"mm.jpg"];CGImageRef imageRef = bigImage.CGImage;CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);CGSize size;size.width = 150;size.height = 150;UIGraphicsBeginImageContext(size);CGContextRef context = UIGraphicsGetCurrentContext();CGContextDrawImage(context, myImageRect, subImageRef);UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];UIGraphicsEndImageContext();return smallImage;}?
?
轉載于:https://my.oschina.net/zhaodacai/blog/790661
總結
以上是生活随笔為你收集整理的swift3.0截取View生成图片 图片截取成新图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷P2746 [USACO5.3]校园
- 下一篇: 约瑟夫环:递归算法