直接上源码!
UIView * screenView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO];
UIGraphicsBeginImageContextWithOptions(screenView.frame.size, YES, [[UIScreen mainScreen] scale]);
[screenView drawViewHierarchyInRect:screenView.bounds afterScreenUpdates:YES];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
1.保存到相册
UIImageWriteToSavedPhotosAlbum(screenImage, nil, nil, nil);
2.保存到任意路径
NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; //沙盒目录,可以自己修改
NSString*filePath=[pathDocuments stringByAppendingPathComponent:@"contact.png"];
NSData * creenData =UIImagePNGRepresentation(screenImage);
NSFileManager * fileManager=[NSFileManager defaultManager];
[fileManager createFileAtPath:filePath contents:creenData attributes:nil];
搞了我一天半,总算出来了,记录一下分享一下!!
菜鸟走向大牛,大家共同前进,如果觉得不错,请给个赞/关注。
一起交流学习,有问题随时欢迎联系,邮箱:383708669@qq.com
网友评论