iOS如何把一个view生成一张图片,并且保存到本地

因为涉及到访问相册,所以先在plist文件里面添加NSPhotoLibraryUsageDescription允许应用程序访问你的相册

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

// self.testView:要生成为图片的view

UIGraphicsBeginImageContextWithOptions(self.testView.bounds.size, 0, [[UIScreen mainScreen] scale]);

[self.testView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);

});

}

- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo

{

if (!error) {

NSLog(@"成功");

}else {

NSLog(@"失败 - %@",error);

}

}

你可能感兴趣的:(iOS如何把一个view生成一张图片,并且保存到本地)