ios系统截屏并重命名保存到任意位置

直接上源码!

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];



搞了我一天半,总算出来了,记录一下分享一下!!

菜鸟走向大牛,大家共同前进,如果觉得不错,请给个赞/关注。

一起交流学习,有问题随时欢迎联系,邮箱:[email protected]

你可能感兴趣的:(ios系统截屏并重命名保存到任意位置)