0

因此,我正在编写的应用程序允许用户从他们的相机胶卷上传照片,并显示在 UIImageView 中。我有一个“保存”按钮,可以在按下时保存图像。我还有一个“编辑”按钮,当点击它时,它允许用户点击照片,它将被删除。这是我遇到问题的地方,图像从视图中删除,但似乎没有从数组中删除。当我关闭并重新启动应用程序时,照片再次出现。我对 Objective-C 很陌生,我很惊讶我能做到这一点,所以我在弄清楚如何从数组中删除它时遇到了问题。

另外,作为旁注,如果我从相机胶卷中抓取 3 张图像,删除中间的一张,然后按保存按钮,在关闭并重新启动应用程序后,它只会显示应有的 2 张照片。只有当我先按保存,然后尝试删除它们时,才会出现有关重新启动后再次出现图像的问题。

这就是我删除它们的方式。我在每个 UIImageView 上都有一个不可见的按钮,当按下 Edit 按钮时,它会取消隐藏它们。我已经标记了按钮和 UIImageViews,当按下其中一个按钮时,会触发:

- (IBAction)deleteButtonPressed:(id)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]) {

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

        ((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
    }

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

仅供参考,这就是我保存图像的方式:

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

            [self.array addObject:imageView];

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

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

            return;
        }
        ...
        ...
        }

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



        for (UIImageView *imageViewToSave in self.array) {

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

            NSString *imagePath = [docsDir stringByAppendingPathComponent:imageName];

            [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:NO];
        }

    }

这就是我加载它们的方式:

    -(void)viewDidLoad {

        self.array = [NSMutableArray array];
        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 if (!imageView2.image) {
                    imageView2.image = loadedImage;
                  }
                else if (!imageView3.image) {
                    imageView3.image = loadedImage;

                }

            }
        }
    }

更新:这是我当前的代码:

- (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]) {

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

            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
            NSString *documentsPath = [paths objectAtIndex:0];
            NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Image%i.png",view.tag]];
            NSFileManager *fileManager = [NSFileManager defaultManager];
            [fileManager removeItemAtPath:filePath error:NULL];
        }

        ((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
    }

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

2 回答 2

2

您的数组与此无关。无论如何,它会在下次启动时被覆盖。我认为您从未从文件夹中删除图像,因此下次启动时会在 viewDidLoad 方法中再次读取它。

于 2012-04-16T02:56:05.573 回答
2

当您从数组中删除图像时[self.array removeObject:view];,您还没有从目录中删除图像本身。

从以下位置删除对象后,您可能希望执行以下操作array

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"THE_IMAGE_NAME_HERE.png"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
[fileManager removeItemAtPath:filePath error:&error];

if (error)
NSLog(@"Error: %@",error);
于 2012-04-16T02:57:12.437 回答