我正在尝试制作一张拍照片的应用程序,将它们保存在照片库中,选择现有照片并删除照片。除了删除部分之外,我做了所有这些。你能救我吗?
#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
答案 0 :(得分:0)
在删除图像时查看此堆栈溢出帖子,但该链接中的信息表明只能删除应用程序创建的图像。