objective-c - 如何保存这组图像?

标签 objective-c ios image tags

我是编程的新手,我直接投入了一个项目(我知道这不是最明智的做法,但我边学边做)。我正在编写的应用程序有 10 个 UIImageViews,显示用户相机胶卷中的图片。我正在使用的代码需要每个 UIImageViews 都有标签。我目前正在使用 NSData 保存数组图像,效果很好,但我不能再使用这种方法,因为 NSData 不支持使用标签。我也不能使用 NSUserDefaults,因为我不能将图像保存到 plist。这是我尝试执行此操作的方法(使用 NSData 方法,该方法有效,但我必须对其进行编辑,以便我的标签起作用。)

这是我当前的代码:

- (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];

}

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

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

       -(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 属性添加到数组中,这样您就可以恢复标记。所以不要写

[self.array addObject:imageView.image];

会是

[self.array addObject:imageView];

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

-(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 方法

-(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() 函数需要大量时间并且不是瞬时的。你应该意识到这一点。最好在其他地方编写一些这样的代码,并且比在应用程序后台更早地完成。前世今生

关于objective-c - 如何保存这组图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10099233/

相关文章:

ios - 为什么纬度和经度在 ios 9 中显示为零?

ios - 当玩家 "Remove"一场比赛正在进行时如何从 GKTurnBasedMatchmakerViewController 获得通知

使用 getImageData() 时,html5 Canvas 总是返回 8 位颜色

ios - 挂起(卡住)问题 UISemanticContentAttribute = .ForceRightToLeft

Android - 在 Canvas 上淡出位图图像

java - 使用 Swing 的 Java 图形用户界面

iphone - 如何正确保留对 ActionSheet 委托(delegate)使用的对象的引用?

iphone - 在 iPad 上选择图像

objective-c - 如何使用 Objective-C 获取任何文件的文件类型描述?

ios - 苹果开发库 : no Scene Kit guide?