0

I'm trying to do an app which take a photo, save them at photo library, choose an existing one and delete the photo. I made all of it except the deleting part. Can you help me?

#pragma mark - Event

- (void)TakePhoto {

    UIImagePickerController * picker = [[UIImagePickerController alloc] init];

    picker.delegate = self;

    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    picker.allowsEditing = YES;

    [self presentViewController: picker animated: YES completion:NULL];

    self.newMedia = YES;
}

-(void)ChooseExisting {

    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.allowsEditing = YES;

    [self presentViewController: picker animated: YES completion:NULL];
    self.newMedia = NO;
}



#pragma mark - UIImagePickerControllerDelaegate

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSString *mediaType = info[UIImagePickerControllerMediaType];

    [self dismissViewControllerAnimated:YES completion:NULL];

    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    self.imageView.image = image;

    if(self.newMedia) {
       UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:finishedSavingWithError:contextInfo:),nil);

    }
}

    -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

        [self dismissViewControllerAnimated:YES completion:nil];
    }

#pragma mark - Action Sheet

    - (void)showActionSheet:(id) sender {
        NSString *actionSheetTitle = @"Action Sheet Demo";
        NSString *destructiveTitle = @"Delete";
        NSString *other1 = @"Take Photo";
        NSString *other2 = @"Chose From Existing";
        NSString *cancelTitle = @"Cancel";

        UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                      initWithTitle:actionSheetTitle
                                      delegate:self
                                      cancelButtonTitle:cancelTitle
                                      destructiveButtonTitle:destructiveTitle
                                      otherButtonTitles:other1, other2, nil];
        [actionSheet showInView:self.view];

    }


    -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
        NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
        if  ([buttonTitle isEqualToString:@"Delete"]) {
            NSLog(@"Destructive pressed --> Delete Something");
        }
        if ([buttonTitle isEqualToString:@"Take Photo"]) {
            [self TakePhoto];
        }
        if ([buttonTitle isEqualToString:@"Chose From Existing"]) {
            [self ChooseExisting];
        }

        if ([buttonTitle isEqualToString:@"Cancel Button"]) {
            NSLog(@"Cancel pressed --> Cancel ActionSheet");
        }
    }

#pragma mark - Alert

    -(void)image:(UIImage *)image finishedSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
    {
        if (error) {
            UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle: @"Save failed"
                                  message: @"Failed to save image"
                                  delegate: nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
            [alert show];
        }
    }

#pragma mark - Life Cycle

    -(void)viewDidLoad {
        [super viewDidLoad];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(20.0f, 186.0f, 280.0f, 88.0f);
        [button setTitle:@"Show Action Sheet" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.tintColor = [UIColor darkGrayColor];
        [button addTarget:self action:@selector(showActionSheet:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];

        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

            UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                  message:@"Device has no camera"
                                                                 delegate:nil
                                                        cancelButtonTitle:@"OK"
                                                        otherButtonTitles: nil];

            [myAlertView show];

        }



    }



@end
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

See this stack overflow post on deleting images, but the information in that link states that only images created by your app can be deleted.

Delete a photo from the user's photo library?

Community
  • 1
  • 1
roninSTI
  • 151
  • 1
  • 6