如何保存此图像数组? [英] How Can I Save This Array of Images?

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

问题描述

我对编程非常陌生,我跳进了一个项目(我知道这不是最聪明的事情,但我正在学习,因为我去了)。我正在编写的应用程序有10个UIImageViews,用于显示用户相机胶卷的图片。我正在使用的代码需要每个UIImageViews都有标签。我目前正在使用NSData来保存数组图像,它工作得很好,但我不能再使用这种方法了,因为NSData不支持使用标签。我也不能使用NSUserDefaults,因为我无法将图像保存到plist。以下是我尝试这样做的方法(使用NSData方法,但是我必须编辑它以便我的标签可以工作。)

I'm very new to programming, and I jumped right into a project (I know thats not the smartest thing to do, but I'm learning as I go). The app that I'm writing has 10 UIImageViews that display a picture from the users camera roll. The code I'm using needs each of the UIImageViews to have tags. I'm currently using NSData to save the array images, and it works great, but I can't use this method anymore because NSData doesn't support the use of tags. I also can't use NSUserDefaults, because I can't save images to a plist. Here is how I'm attempting to do this (using the NSData method, which works but I have to edit this so that my tags work.)

这是我的当前代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
    if (imageView.image == nil) {
        imageView.image = img;

        [self.array addObject:imageView.image];

        [picker dismissModalViewControllerAnimated:YES];
        [self.popover dismissPopoverAnimated:YES];
        return;

    }

    if (imageView2.image == nil) {
        imageView2.image = img;
        NSLog(@"The image is a %@", imageView);
        [self.array addObject:imageView2.image];

        [picker dismissModalViewControllerAnimated:YES];
        [self.popover dismissPopoverAnimated:YES];
        return;
    }
    ...


- (void)applicationDidEnterBackground:(UIApplication*)application {
    NSLog(@"Image on didenterbackground: %@", imageView);

    [self.array addObject:imageView.image];
    [self.array addObject:imageView2.image];

    [self.user setObject:self.array forKey:@"images"];
    [user synchronize];

}

- (void)viewDidLoad
{
    self.user = [NSUserDefaults standardUserDefaults];
    NSLog(@"It is %@", self.user);
    self.array = [[self.user objectForKey:@"images"]mutableCopy];
    imageView.image = [[self.array objectAtIndex:0] copy];
    imageView2.image = [[self.array objectAtIndex:1] copy];    

    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationDidEnterBackground:)
                                                 name:UIApplicationDidEnterBackgroundNotification
                                               object:app];

    [super viewDidLoad];

}

有关如何编辑此代码的任何帮助或建议,以便我可以保存图片,同时使用标签非常感谢,谢谢!

Any help or suggestions on how to edit this code so that I can save the images, while using tags is much appreciated, thanks!

编辑:这是我的更新代码:

       -(IBAction)saveButtonPressed:(id)sender {
    NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) objectAtIndex:0];

    for (UIImageView *imageView in self.array) {

        NSInteger tag = self.imageView.tag;
        UIImage *image = self.imageView.image;
        NSString *imageName = [NSString stringWithFormat:@"Image%i.png",tag];

        NSString *imagePath = [docsDir stringByAppendingPathComponent:imageName];
        [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
    }
NSLog(@"Saved Button Pressed");
}



- (void)applicationDidEnterBackground:(UIApplication*)application {

}


-(void)viewDidLoad {

    NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];

    NSArray *docFiles = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:docsDir error:NULL];

    for (NSString *fileName in docFiles) {

        if ([fileName hasSuffix:@".png"]) {
            NSString *fullPath = [docsDir stringByAppendingPathComponent:fileName];
            UIImage *loadedImage = [UIImage imageWithContentsOfFile:fullPath];

            if (!imageView.image) {
                imageView.image = loadedImage;
            } else {
                imageView2.image = loadedImage;
            }
        }
    }
}


推荐答案

您需要使用快速枚举来解析数组的对象,并按顺序将每个对象写入磁盘。首先,您需要将UIImageView对象添加到数组而不是UIImageView的UIImage属性,因此您可以恢复标记。所以不要写

You need to use "Fast Enumeration" to parse the array's objects, and write each object to disk sequentially. First, you're going to need to add the UIImageView objects to the array instead of the UIImage property of the UIImageView, so you can recover the tag. So instead of writing

[self.array addObject:imageView.image];

这将是

[self.array addObject:imageView];

尝试按照我的代码。我在每一行插入了注释以提供帮助。

Try to follow along with my code. I inserted comments on each line to help.

-(void)applicationDidEnterBackground:(UIApplication *)application {
    //Obtain the documents directory
    NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainmask,YES) objectAtIndex:0];
    //begin fast enumeration
    //this is special to ObjC: it will iterate over any array one object at a time
    //it's easier than using for (i=0;i<array.count;i++)
    for (UIImageView *imageView in self.array) {
        //get the imageView's tag to append to the filename
        NSInteger tag = imageView.tag;
        //get the image from the imageView;
        UIImage *image = imageView.image;
        //create a filename, in this case "ImageTAGNUM.png"
        NSString *imageName = [NSString stringWithFormat:@"Image%i.png",tag];
        //concatenate the docsDirectory and the filename
        NSString *imagePath = [docsDir stringByAppendingPathComponent:imageName];
        [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
    }
}

要从磁盘加载图像,你将拥有查看你的viewDidLoad方法

To load the images from disk, you'll have to look at your viewDidLoad method

-(void)viewDidLoad {
    //get the contents of the docs directory
    NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainmask,YES) objectAtIndex:0];
    //Get the list of files from the file manager
    NSArray *docFiles = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:docsDir error:NULL]);
    //use fast enumeration to iterate the list of files searching for .png extensions and load those
    for (NSString *fileName in docFiles) {
        //check to see if the file is a .png file
        if ([fileName hasSuffix:@".png"]) {
            NSString *fullPath = [docsDir stringByAppendingPathComponent:fileName];
            UIImage *loadedImage = [UIImage imageWithContentsOfFile:fullPath];
            //you'll have to sort out how to put these images in their proper place
            if (!imageView1.image) {
                imageView1.image = loadedImage;
            } else {
                imageView2.image = loadedImage;
            }
        }
    }
}

希望这有帮助

您需要注意的一件事是,当应用程序进入后台时,它有大约5秒的时间来清除它的行为,然后才会暂停。 UIPNGRepresentation()函数需要大量时间并且不是瞬时的。你应该知道这一点。在其他地方编写一些代码可能会比在应用程序后台更早地完成。 FWIW

One thing you need to be aware of is that when an app enters the background it has about 5 seconds to clean up its act before it's suspended. The UIPNGRepresentation() function takes a significant amount of time and is not instantaneous. You should be aware of this. It would probably be better to write some of this code in other places and do it earlier than at app backgrounding. FWIW

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

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