1

我有一个像照片库一样的应用程序。我正在实现用户删除照片的能力,方法是在每个 UIImageView 上放置一个不可见的按钮,并在他们点击按钮时调用 removeObject。此代码运行良好,但它依赖于标签。我需要在界面构建器中标记每个 UIImageView / UIButton 才能使其正常工作。所以,我现在正试图以我的标签仍然有效的方式保存图像,这不包括使用 NSData。

所以我完全不知道现在该做什么。我对编程非常非常陌生,我什至能做到这一点感到震惊。非常感谢有关什么或如何编辑我的代码以使其工作的任何帮助或建议,谢谢!

这是我的整个文件仅供参考:

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

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

        [self.array addObject:imageView4.image];

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

        [self.array addObject:imageView5.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.array addObject:imageView4.image];
    [self.array addObject: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 = [[self.array objectAtIndex:0] copy];
    imageView2.image = [[self.array objectAtIndex:1] copy];
    imageView3.image = [[self.array objectAtIndex:2] copy];
    imageView4.image = [[self.array objectAtIndex:3] copy];
    imageView5.image = [[self.array objectAtIndex:4] copy];



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

    [super viewDidLoad];

}


// This is when the user taps on the image to delete it.
- (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"];
}

@end
4

2 回答 2

1

前面的评论是准确的;请考虑如何扩大规模。

也就是说,您需要以某种方式存储和加载图像,这总是意味着将您的 UIImage 对象转换为 NSData。无论您将它们存储在 NSUserDefaults、文件还是 Core Data 中,这都适用。

UIImage 支持 NSCoding,所以你可以使用它。阅读有关如何使用它的信息,因为您最终会需要它。或者,如果您知道自己总是想使用 PNG 或 JPEG 格式,那么可以使用函数UIImagePNGRepresentation(UIImage *image)UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality). 要将 NSData 对象转换回 UIImage,请使用[UIImage imageWithData:NSData*]以下是您在 applicationDidEnterBackground 中可以拥有的内容:

NSMutableArray *dataArray = [NSMutableArray array];
[dataArray addObject:UIImagePNGRepresentation(imageView.image)];
[dataArray addObject:UIImagePNGRepresentation(imageView2.image)];
[dataArray addObject:UIImagePNGRepresentation(imageView3.image)];
[self.user setObject:dataArray forKey:@"images"];
[self.user synchronize];

然后,在 viewDidLoad 期间检索这些:

[self.array removeAllObjects];
NSArray *dataArray = [self.user objectForKey:@"images"];
for (NSData *imageData in dataArray)
    [self.array addObject:[UIImage imageWithData:imageData]];

这将解决您当前的问题,但请不要将其视为永久解决方案。考虑:

  1. 将图像存储在文件或核心数据中,因为 NSUserDefaults 不应该包含大量内容。
  2. 使用 UITableView 显示图像,因此您可以使用任意数字而不是硬编码为三个。
  3. 如果 UITableView 没有您需要的布局,请创建一个自定义 UIView 子类,该子类显示一组图像并适当地响应点击。
  4. 确保您的应用程序可以检测到它可能被挂起或关闭的所有方式。您可能不会总是收到 ApplicationDidEnterBackground 通知,或者您可能没有时间在它发生后保存所有数据。如果你有多个 UIViewControllers,这一个可能会在应用程序本身没有收到通知的情况下被卸载。
于 2012-04-07T01:17:03.250 回答
1

您似乎正在使用该标签,以便您可以识别正在选择的图像视图。您在每个图像顶部都有一个“隐形”按钮?那正确吗?我假设这样你就可以处理一个点击,它选择通过按钮显示的图像?

有很多方法可以做到这一点,但一个简单的解决方案是只识别水龙头,然后“找到”该水龙头下的图像视图。从情节提要中将 UITapGestureRecognizer 拖放到您的控制器上。按住 Ctrl 键将其拖入控制器的代码中,它将创建一个操作方法。填写类似这样的内容...

  - (IBAction)tapGesture:(UITapGestureRecognizer*)gesture
  {
    CGPoint tapLocation = [gesture locationInView: self.galleryView];
    for (UIImageView *imageView in self.galleryView.subviews) {
      if (CGRectContainsPoint(imageView.frame, tapLocation)) {
        // This is the imageView that was tapped on!
        // Do whatever you want with it now that you found it.
      }
    }
  }
于 2012-04-10T20:28:31.667 回答