• iphone document 图片存储和读取


    存:

    Java代码 复制代码 收藏代码
    1. //此处首先指定了图片存取路径(默认写到应用程序沙盒 中)  
    2.   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
    3.     
    4.   //并给文件起个文件名  
    5.   NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"pin.png"];  
    6.   BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];  
    7.   if (blHave) {  
    8.       NSLog(@"already have");  
    9.       return ;  
    10.   }  
    11.   //此处的方法是将图片写到Documents文件中 如果写入成功会弹出一个警告框,提示图片保存成功  
    12.   NSString *strPathOld = [[NSBundle mainBundle] pathForResource:@"pin" ofType:@"png"];  
    13.   NSData *data = [NSData dataWithContentsOfFile:strPathOld];  
    14.   BOOL result = [data writeToFile:uniquePath atomically:YES];  
    15.   if (result) {  
    16.       NSLog(@"success");  
    17.   }else {  
    18.       NSLog(@"no success");  
    19.   }  
      //此处首先指定了图片存取路径(默认写到应用程序沙盒 中)
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        
        //并给文件起个文件名
        NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"pin.png"];
        BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
        if (blHave) {
            NSLog(@"already have");
            return ;
        }
        //此处的方法是将图片写到Documents文件中 如果写入成功会弹出一个警告框,提示图片保存成功
        NSString *strPathOld = [[NSBundle mainBundle] pathForResource:@"pin" ofType:@"png"];
        NSData *data = [NSData dataWithContentsOfFile:strPathOld];
        BOOL result = [data writeToFile:uniquePath atomically:YES];
        if (result) {
            NSLog(@"success");
        }else {
            NSLog(@"no success");
        }
     

    取:

    Java代码 复制代码 收藏代码
    1. NSFileManager *fileManager = [NSFileManager defaultManager];  
    2.   NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,                                                                          NSUserDomainMask, YES);  
    3.   NSString *documentsDirectory = [paths objectAtIndex:0];  
    4.   NSString *filePath2 = [documentsDirectory stringByAppendingPathComponent:@"pin.png"];  
    5.   UIImage *img = [UIImage imageWithContentsOfFile:filePath2];  
    6.   [image setImage:img];  
    7.   NSLog(@"图片:::::::::%@",image.image);  
      NSFileManager *fileManager = [NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,                                                                          NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *filePath2 = [documentsDirectory stringByAppendingPathComponent:@"pin.png"];
        UIImage *img = [UIImage imageWithContentsOfFile:filePath2];
        [image setImage:img];
        NSLog(@"图片:::::::::%@",image.image);
     

    删:

    Java代码 复制代码 收藏代码
    1. NSFileManager* fileManager=[NSFileManager defaultManager];  
    2.    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
    3.      
    4.    //文件名  
    5.    NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"pin.png"];  
    6.    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];  
    7.    if (!blHave) {  
    8.        NSLog(@"no  have");  
    9.        return ;  
    10.    }else {  
    11.        NSLog(@" have");  
    12.        BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];  
    13.        if (blDele) {  
    14.            NSLog(@"dele success");  
    15.        }else {  
    16.            NSLog(@"dele fail");  
    17.        }  
    18.          
    19.    }  
     NSFileManager* fileManager=[NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        
        //文件名
        NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"pin.png"];
        BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
        if (!blHave) {
            NSLog(@"no  have");
            return ;
        }else {
            NSLog(@" have");
            BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
            if (blDele) {
                NSLog(@"dele success");
            }else {
                NSLog(@"dele fail");
            }
            
        }
    
    
  • 相关阅读:
    《JavaScript 源码分析》之 jquery.unobtrusive-ajax.js
    《JavaScript高级程序设计》读书笔记 2
    《JS设计模式笔记》构造函数和工厂模式创建对象
    《ES6基础教程》之 map、forEach、filter indexOf 用法
    《JS设计模式笔记》 5,适配器模式
    51Nod 1058 N的阶乘的长度
    ACM总结——2017区域赛网络赛总结
    ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 题目9 : Minimum
    hiho一下 第168周
    Fast Matrix Calculation HDU
  • 原文地址:https://www.cnblogs.com/lovewx/p/4073978.html
Copyright © 2020-2023  润新知