0

我正在开发一个行为类似于照片库的应用程序,并且我正在实现让用户从他们的画廊中删除照片的选项。为此,我决定在每张图片上放置一个隐形按钮。当用户点击“编辑”按钮时,每张图片上隐藏的删除按钮将变为活动状态。为了简单起见,我在每个隐藏按钮上使用了相同的 IBOutlet,并且我在 Interface Builder 中适当地标记了每个按钮。当用户点击图片上的按钮时,会出现一个警告视图,询问他们是否真的要删除它。如果他们单击“是”,我将调用 removeObjectAtIndex。这是我正在使用的代码:

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

}

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


    if (buttonIndex != [alertView cancelButtonIndex]) {
        NSLog(@"User Clicked Yes.");
        [self.array removeObjectAtIndex: alertView.tag];
    }
    [self.user setObject:array forKey:@"images"];
}

这里的问题是,当我在警报视图中单击“是”时,什么也没有发生。但是,如果我再次点击图像并单击“是”,应用程序崩溃,并且调试状态: 由于未捕获的异常 'NSRangeException' 导致应用程序终止,原因:'-[__NSCFArray removeObjectAtIndex:]: index (0) Beyond bounds (0)'所以,我不知道从哪里开始,我对编程还是很陌生,一切对我来说都是正确的。非常感谢任何帮助,谢谢!

这是我将它们添加到数组中的方法:

////start of saving////
- (void)viewWillAppear:(BOOL)animated
{

    self.user = [NSUserDefaults standardUserDefaults];

    self.array = [[self.user objectForKey:@"images"]mutableCopy];
    while(self.array == nil)
    {
        [self.user setObject:[NSMutableArray arrayWithObject:@""] forKey:@"images"];
        self.array = [[self.user objectForKey:@"images"]mutableCopy];
        NSLog(@"%@",@"attempting to create an array to store the images in");
    }

}

- (void)applicationDidEnterBackground:(UIApplication*)application {
    NSLog(@"Image on didenterbackground: %@", imageView);
    self.array = [NSMutableArray arrayWithObject:[NSData dataWithData:UIImagePNGRepresentation(imageView.image)]];

    [self.array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView2.image)]];
     [self.array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView3.image)]];
      [self.array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView4.image)]];
       [self.array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView5.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] initWithData:[self.array objectAtIndex:0]];
        imageView2.image = [[UIImage alloc] initWithData:[self.array objectAtIndex:1]];
        imageView3.image = [[UIImage alloc] initWithData:[self.array objectAtIndex:2]];
        imageView4.image = [[UIImage alloc] initWithData:[self.array objectAtIndex:3]];
        imageView5.image = [[UIImage alloc] initWithData:[self.array objectAtIndex:4]];
        imageView6.image = [[UIImage alloc] initWithData:[self.array objectAtIndex:5]];


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

    }

- (void)viewDidUnload
    {
        self.user = nil;
    }

////end of saving



///// shows the hidden and invisible "delete" button over each photo.
- (IBAction)editButtonPressed:(id)sender {
    grabButton.hidden = YES;
    editButton.hidden = YES;
    backToGalleryButton.hidden = NO;
    tapToDeleteLabel.hidden = NO;
    deleteButton1.hidden = NO;
}


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

}

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


    if (buttonIndex != [alertView cancelButtonIndex]) {
        NSLog(@"User Clicked Yes.");
        NSLog(@"Array: %@, index: %d", self.array, alertView.tag);
        [self.array removeObjectAtIndex: alertView.tag];
    }
    [self.user setObject:array forKey:@"images"];
}
@end

编辑:这是我用来将对象从用户相机胶卷放入 UIImageView 的代码:

- (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;
        [picker dismissModalViewControllerAnimated:YES];
        [self.popover dismissPopoverAnimated:YES];
        return;

    }

    if (imageView2.image == nil) {
        imageView2.image = img;
        [picker dismissModalViewControllerAnimated:YES];
        [self.popover dismissPopoverAnimated:YES];
        return;
    }

    if (imageView3.image == nil) {
        imageView3.image = img;
        [picker dismissModalViewControllerAnimated:YES];
        [self.popover dismissPopoverAnimated:YES];
        return;
    }

    if (imageView4.image == nil) {
        imageView4.image = img;
        [picker dismissModalViewControllerAnimated:YES];
        [self.popover dismissPopoverAnimated:YES];
        return;
    }

}

当我添加 NSLog(@"Array: %@, index: %d", self.array, alertView.tag); 就在 removeAtIndex 之前,控制台说2012-04-03 18:39:39.066 AppName[1631:f803] Array: (null), index: 0.这可能是原因吗?我不确定为什么会这样,我认为代码看起来不错。

4

2 回答 2

3

从阵列中删除图像并不是您必须采取的唯一步骤。您的代码对于从数组中删除图像是正确的,这就是为什么您第二次将图像越界,但您还需要从 UI 中删除图像,这样用户就不能再删除不存在的图像。

于 2012-04-03T23:51:32.150 回答
2

这段代码有很多奇怪之处,但我认为问题在于您没有在 viewwillappear 和 viewdidload 函数上调用 super 。我会摆脱您的 viewWillAppear 方法,因为它没有任何用处。

于 2012-04-04T00:29:34.880 回答