iOS新手学习

ios截屏及保存图片到本地

2017-03-03  本文已影响1437人  拿铁加冰

截取当前屏幕显示的内容,并保存在本地:

1、项目添加:Photos.framework,AssetsLibrary.framework;

2、点击截屏按钮,调取下面的方法:

-(void)jiepingBtn{

UIImage * image = [self captureImageFromView:self.view];

ALAssetsLibrary * library = [ALAssetsLibrary new];

NSData * data = UIImageJPEGRepresentation(image, 1.0);

[library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:nil];

}

3、captureImageFromView的实现:

//截图功能

-(UIImage *)captureImageFromView:(UIView *)view{

UIGraphicsBeginImageContextWithOptions(self.view.frame.size,NO, 0);

[[UIColor clearColor] setFill];

[[UIBezierPath bezierPathWithRect:self.view.bounds] fill];

CGContextRef ctx = UIGraphicsGetCurrentContext();

[self.view.layer renderInContext:ctx];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;}

4、现在你就可以在你本地图库里看你刚才截取的图片了。

5、方法的简单解释:

UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数。

上一篇下一篇

猜你喜欢

热点阅读