美文网首页
截屏并重命名保存到任意路径下

截屏并重命名保存到任意路径下

作者: _Hal_ | 来源:发表于2017-09-12 15:25 被阅读10次

截屏并重命名保存到任意路径下

// 截屏并重命名保存到任意路径下
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UIView *screenView = [[UIScreen mainScreen]snapshotView AfterScreenUpdates:NO];
    UIGraphicsBeginImageContextWithOptions(screenView.frame.size, YES, [[UIScreen mainScreen]scale]);
    [screenView drawViewHierarchyInRect:screenView.bounds afterScreenUpdates:YES];
    UIImage *image  = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //  1.保存到相册
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    
    

    //  2.保存到任意路径
    
    NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; //沙盒目录,可以自己修改
    
    NSString*filePath=[pathDocuments stringByAppendingPathComponent:@"contact.png"];
    
    NSData * creenData =UIImagePNGRepresentation(image);
    
    NSFileManager * fileManager=[NSFileManager defaultManager];
    
    [fileManager createFileAtPath:filePath contents:creenData attributes:nil];
}

相关文章

网友评论

      本文标题:截屏并重命名保存到任意路径下

      本文链接:https://www.haomeiwen.com/subject/acvcsxtx.html