最近的项目中运用到了这两个功能,所以记录一下。做了一个小工程将两个方法结合到了一起
-(void)viewDidLoad {
[super viewDidLoad];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
btn.frame =CGRectMake(30, 30, 30, 30);
[self.view addSubview:btn];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)btnClick:(UIButton *)sender
{
[self savePhoto];
}
// 保存图片到相册功能,ALAssetsLibraryiOS9.0 以后用photoliabary 替代,
-(void)savePhoto
{
UIImage * image = [self captureImageFromView:self.view];
ALAssetsLibrary * library = [ALAssetsLibrary new];
NSData * data = UIImageJPEGRepresentation(image, 1.0);
[library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:nil];
}
//截图功能
-(UIImage *)captureImageFromView:(UIView *)view
{
CGRect screenRect = [view bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[view.layer renderInContext:ctx];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
文/镇屌要逆袭(简书作者)
原文链接:http://www.jianshu.com/p/a82aac64500e
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。