0

我将图像存储在 NSMutableArray 中,现在我正试图让它们显示在 viewDidLoad 中。我尝试调用 initWithContentsOfFile,但这似乎不起作用。这是代码的样子: imageView.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:0]];

我不确定我应该使用什么而不是 initWithContentsOfFile 来加载保存的图像,我什至不确定是否可以通过用户默认值将图像保存在 plist 中。我已经研究了一段时间了,但无济于事。非常感谢任何帮助,谢谢!

编辑:这是附加代码:

- (IBAction)grabImage {
    self.imgPicker = [[UIImagePickerController alloc] init];
    self.imgPicker.delegate = self;
    self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        _popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
        [_popover presentPopoverFromRect:self.imageView.bounds inView:self.imageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    } 

    else {
        [self presentModalViewController:imgPicker animated:YES];
    }
    [self.imgPicker resignFirstResponder];
}
// Sets the image in the UIImageView
- (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;
    }

    if (imageView3.image == nil) {
        imageView3.image = img;

        [self.array addObject:imageView3.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.array addObject:imageView3.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 = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:0]];
        imageView2.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:1]];
        imageView3.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:2]];




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

        backToGalleryButton.hidden = YES;
        tapToDeleteLabel.hidden = YES;
        deleteButton1.hidden = YES;
        [super viewDidLoad];

    }

编辑:这就是我标记图像并根据它们的标签删除它们的方式:

- (IBAction)deleteButtonPressed:(id)sender {
    NSLog(@"Sender is %@", sender);
    UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete"
                                                              message:@"Are you sure you want to delete this photo?"
                                                             delegate:self
                                                    cancelButtonTitle:@"No"
                                                    otherButtonTitles:@"Yes", nil];
    [deleteAlertView show];

    int imageIndex = ((UIButton *)sender).tag;
    deleteAlertView.tag = imageIndex;
}

- (UIImageView *)viewForTag:(NSInteger)tag {
    UIImageView *found = nil;
    for (UIImageView *view in self.array) {
        if (tag == view.tag) {
            found = view;
            break;
        }
    }
    return found;
}

- (void)alertView: (UIAlertView *) alertView 
clickedButtonAtIndex: (NSInteger) buttonIndex
{


    if (buttonIndex != [alertView cancelButtonIndex]) {
        NSLog(@"User Clicked Yes. Deleting index %d of %d", alertView.tag, [array count]);
        NSLog(@"The tag is %i", alertView.tag);

        UIImageView *view = [self viewForTag:alertView.tag];
        if (view) {
            [self.array removeObject:view];
        }

        NSLog(@"After deleting item, array count  = %d", [array count]);
    NSLog(@"Returned view is :%@, in view: %@", [self.view viewWithTag:alertView.tag], self.view);
        ((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
    }

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

2 回答 2

2

问题是您不能将图像存储在属性列表中,这是您将其保存在用户默认值中时尝试执行的操作。您需要使用存档器将图像转换为可以存储的 NSData 对象。

于 2012-04-06T00:37:14.043 回答
0

看起来您没有将有效的图像路径传递给初始化方法。确保路径正确,并且包含图像扩展名。

但实际上,您不应该initWithContentsOfFile:在这种情况下调用,因为UIImageView' 的image属性会在您设置图像时保留它。这通常会导致内存泄漏(除非您使用自动引用计数)。改用静态初始化程序之一,例如imageNamed:,它具有使用系统缓存的额外好处,并且还根据设备的特性自动加载正确版本的图像(例如,它将加载更高分辨率的变体如果设备具有视网膜显示器,则为图像)。

于 2012-04-06T00:03:11.150 回答