美文网首页
iOS把UIImage对象保存到本地沙盒

iOS把UIImage对象保存到本地沙盒

作者: 脑袋好重 | 来源:发表于2018-07-06 10:43 被阅读0次

    1. 保存

    - (void)saveImage:(UIImage*)image {

    NSArray*paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);NSString*filePath = [[paths objectAtIndex:0]stringByAppendingPathComponent:[NSStringstringWithFormat:@"demo.png"]];// 保存文件的名称

    BOOLresult =[UIImagePNGRepresentation(image)writeToFile:filePath  atomically:YES];// 保存成功会返回YES

    if(result ==YES) {

        NSLog(@"保存成功");

     }

    }

    2取出保存的图片

    - (void)getImage {

    NSArray*paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);NSString*filePath = [[paths objectAtIndex:0]stringByAppendingPathComponent:[NSStringstringWithFormat:@"demo.png"]];

    // 保存文件的名称

    UIImage*img = [UIImageimageWithContentsOfFile:filePath];

    NSLog(@"=== %@", img);

    }

    相关文章

      网友评论

          本文标题:iOS把UIImage对象保存到本地沙盒

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