【问题标题】:Save images for later use in iPhone保存图像以供以后在 iPhone 中使用
【发布时间】:2012-03-14 14:30:02
【问题描述】:

我想保存用户从相机胶卷中选择的图像,当应用程序退出并重新启动时,我想向用户显示这些图像。我在互联网上搜索并没有找到太多有用的资源。我知道我可以将数据库中的图像写为 blob,但我不想这样做,因为会增加数据库大小。
是否可以保存图像路径,然后使用该路径访问该图像。

【问题讨论】:

  • 这是可能的,通常保存在设备上的 Documents 或 Library/Caches 目录中。
  • 能否提供一些教程或代码?

标签: iphone objective-c ios xcode save


【解决方案1】:

将图像保存到 DocumentDirectory

NSString *imageName = "myImage.png";
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
NSString * documentsDirectoryPath = [paths objectAtIndex:0];

NSString *dataPath = [documentsDirectoryPath  stringByAppendingPathComponent:imageName];
NSData* settingsData = UIImagePNGRepresentation(imageData);

[settingsData writeToFile:dataPath atomically:YES];

编辑:

如果您想在 iTunes->Apps->Documents 中查看 iPhone/iPad 上保存的图像(或共享它们) 只需在 Info.plist 中添加“应用程序支持 iTunes 文件共享”:“是”

【讨论】:

    【解决方案2】:

    你必须使用UIImagePickerController 从相机胶卷中获取媒体

    你会在委托方法中得到图片

    - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    

    在这里您可以获取图像并将其保存为 NSData .. 在文件中,然后下次取回它

    编辑:

    保存

        Data = UIImageJPEGRepresentation(theImage,1.0);
    
           NSString  *imagePath = [ContentFolder stringByAppendingPathComponent:[NSString stringWithFormat:@" %d.plist",Counter]];
            NSLog(@" iamge path %@",imagePath);
    
    
            NSMutableDictionary *objectDictionary = [[NSMutableDictionary alloc]initWithObjectsAndKeys:data,@"data", nil];
    
            [objectDictionary writeToFile:imagePath atomically:YES];
    

    你应该能够根据你的需要改变这些..

    【讨论】:

    • 我知道,我可以在 NSData 中转换图像,但主要问题是保存它们,您能否提供一些代码如何将图像写入文件。谢谢
    猜你喜欢
    • 2012-07-23
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多