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

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

// 截屏并重命名保存到任意路径下
- (void)touchesBegan:(NSSet *)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];
}

你可能感兴趣的:(截屏并重命名保存到任意路径下)