如何在iOS中保存图片 [英] How to save a picture in iOS

查看:101
本文介绍了如何在iOS中保存图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储在其中的图片的数组,这些图片是从相机中取出的,但是当应用程序关闭或最小化时,下次应用程序启动时它们都会消失。修复此问题的最佳方法是什么?

I have an array which has pictures stored in it which are taken from the camera, but when the app is closed or minimised they are all gone next time the app is started. What is the best way to fix this?

推荐答案

为什么不保存图像,而不是将图像添加到数组中到您的应用程序文档文件夹,如下所示:

Instead of adding the image to an array why don't you save the images to your app documents folder like this:

//Save Image to Documents Directory
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
NSString  *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/image.jpg"]];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:imagePath atomically:YES];

现在,您可以随时随地访问图像。

Now you can always access the images anytime you are using the app.

更新#1:

好的说你有多张图片而且你不想覆盖保存在文档目录中的图像。我会做类似以下的事情:

Okay say you have more than one picture and you do not want to overwrite the images saved in your documents directory. I would do something like the following:

首先创建一个返回当前日期和时间的方法

First create a method that returns the current date and time

- (NSString *)currentDateandTime
{
    NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MMddyyyy_HHmmssSS"];
    NSString *dateString = [dateFormat stringFromDate:today];
    [dateFormat release];

    return dateString;
}

正如您所看到的那样,日期缩短到毫秒,这可以防止任何机会覆盖图像要调用该方法,请参阅以下内容:

As you can see the date is down to the millisecond, this prevents any chance of overwriting an image. To call the method see below:

//Set Photo Date
NSString *date = [self currentDateandTime];
Now onto saving the image:
//Save Image to Documents Directory
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
NSString  *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@_image.jpg",date]];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:imagePath atomically:YES];

通过执行上述操作,您已为图像创建了唯一的文件名。例如:10212012_12182245_image.jpg

By doing the above you have created a unique file name for the image. For example: 10212012_12182245_image.jpg

现在检索图像。在保存图像时,您还需要将文件名保存到.PLIST文件中。您可以创建文件名数组并将其作为PLIST文件保存到磁盘。然后,您可以按日期对所有图像进行排序或按日期加载。

Now to retrieve the image. While you are saving the image you would also need to save the file name into a .PLIST file for example. You could create an array of the file names and save them to the disk as a PLIST file. Then you do things like sorting all the images by date or loading them by date.

更新#2:

好的,所以你想创建并保存到PLIST。您可以在图像选择器加载时调用此方法。

Okay so you want to create and save to a PLIST. You can call this when the image picker loads.  

- (void)createPLIST
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [path objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingString:@"/images.plist"];
    //Check to see if the file exists, if not create the file.
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]){
           //Creating empty Image Files Array for later use
           NSMutableArray *imageArray = [[NSMutableArray alloc] init];
           [imageArray writeToFile:plistPath atomically:YES];
       [imageArray release];
        }
}

现在你已经创建了PLIST,你需要保存图像时加载它,然后向其写入数据。我建议创建一个单独的方法,每次拍照时都会调用它。将要发生的是:1。拍摄图片2.)保存带有文件名附加日期和时间的图片3.)将该日期对象传递给下面的方法。下面的方法将向字典添加值并将字典保存回文档目录。另外还要注意,因为我们将所有内容保存到文档文件夹中,所有信息都会备份,因此用户在升级应用程序时不会丢失图像。

Now you have created the PLIST, you will need to load it when saving images and then write data to it. I would suggest creating a separate method that gets called every time you snap a picture. What is going to happen is this: 1.) Take Picture 2.) Save Picture with date and time attached to the file name 3.) Pass that date object to the method below. The method below is going to add values to the dictionary and save the dictionary back to the documents directory. Also on a side note since we are saving everything to the documents folder all of the information is backed up so the user will not lose their images when upgrading the app.

- (void)createNewGalleryObject:(NSString *)date
{
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [path objectAtIndex:0];
    NSString *plistPath = [NSString stringWithFormat:@"%@/images.plist", documentsDirectory];
    //Load PLIST into mutable array
    NSMutableArray *imageArray = [NSMutableArray arrayWithContentsOfFile:plistPath];

    //Create a dictionary for each image captured that saves basic information about the file for later use
    NSMutableDictionary *newEntry = [[NSMutableDictionary alloc] init];
    [imageArray addObject:newEntry];

    //This is where we write to the dictionary, it is up to you what values to save.
    //Based on what you told me I would save the information shown below.
    //For example Later you can do things like sort by date.
    [newEntry setValue:[NSDate date] forKey:@"Date"];
    [newEntry setValue:[NSString stringWithFormat:@"Documents/%@_originalImage.jpg",date] forKey:@"Original Image"];

    //Now save the array back to the PLIST, the array now contains a new dictionary object.
    //Every time you take a picture a new dictionary object is created
    [imageArray writeToFile:path atomically:YES];
    [newEntry release];
}

好吧,让我们说你现在拍了一堆照片并存储了图像信息到你的文件目录。您可以执行加载数组中所有图像的操作。

Okay lets say you now have taken a bunch of pictures and stored the image information to your documents directory. You can do things like load all of the images in the array.

-(void)loadImages
{
    //Load PLIST
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [path objectAtIndex:0];
    NSString *plistPath = [NSString stringWithFormat:@"%@/images.plist", documentsDirectory];
    //Load PLIST into mutable array
    NSMutableArray *imageArray = [NSMutableArray arrayWithContentsOfFile:plistPath];

    for (NSDictionary *dict in imageArray) {
    //Do whatever you want here, to load an image for example
       NSString *imageFilePath = [NSHomeDirectory() stringByAppendingPathComponent:[dict objectForKey:@"Original Image"]];
       UIImage *image = [UIImage imageWithContentsOfFile:imageFilePath];
    }
}

Rock on。

这篇关于如何在iOS中保存图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆