2017-07-13 5 views
-3
-(void)cameraAndPhotoAlbums{ 
    self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"title" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:nil otherButtonTitles:@"Photo album",@"camera", nil]; 

    [self.actionSheet showInView:self.view]; 
} 

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    if (buttonIndex==0) { 
     UIImagePickerController * imagePicker = [[UIImagePickerController alloc]init]; 
     imagePicker.delegate = self; 
     imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
     [self presentViewController:imagePicker animated:YES completion:nil]; 
    } else if(buttonIndex==1) { 
     UIImagePickerController * imagePicker = [[UIImagePickerController alloc]init]; 
     imagePicker.delegate = self; 
     imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 
     [self presentViewController:imagePicker animated:YES completion:nil]; 
    } 
} 

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ 
    [picker dismissViewControllerAnimated:YES completion:nil]; 
} 

-(void)imagePickerControllerDidCancel:(id)picker{ 
    [picker dismissViewControllerAnimated:YES completion:nil]; 
} 

しかし、フォトアルバムの選択時には、キャンセルは実行されますが、実行しないでください。このアイテムはどこにも書き込めませんでした。エージェントは、ここで...didFinishPickingMediaWithInfoは動作しません

+0

イメージピッカーコントローラの代理人を設定しましたか? – Bilal

+1

質問をもう少し説明してください –

+0

いくつかの追加コードを表示 –

答えて

3

私を驚かせた保存偉大な神は、あなたが勉強するためのサンプルコードです:

ViewController.h

@interface ViewController : UIViewController<UIImagePickerControllerDelegate> 
{ 
    UIImagePickerController * imagePicker; 
} 

@property (strong, nonatomic) IBOutlet UIImageView *imageView; 

- (IBAction)setImageToImageView:(UIButton *)sender; 


@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    NSLog(@"%@",NSHomeDirectory()); 

    imagePicker = [[UIImagePickerController alloc]init]; 
    imagePicker.delegate = self; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


#pragma mark - Image Picker Delegate 

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info 
{ 
    NSLog(@"Image picked"); 


    self.imageView.image = [info valueForKey:@"UIImagePickerControllerOriginalImage"]; 

    if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) 
    { 
     NSData * data = UIImageJPEGRepresentation([info valueForKey:@"UIImagePickerControllerOriginalImage"], 0.5); 

     NSString * path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.JPEG"]; 
     [data writeToFile:path atomically:true]; 

    } 
    else 
    { 
     UIImageWriteToSavedPhotosAlbum([info valueForKey:@"UIImagePickerControllerOriginalImage"], nil, nil, nil); 
    } 

    [self dismissViewControllerAnimated:true completion:nil]; 

} 

- (IBAction)setImageToImageView:(UIButton *)sender 
{ 
    if (sender.tag == 101) 
    { 
     imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    } 
    else 
    { 
     imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    } 

    [self presentViewController:imagePicker animated:true completion:nil]; 

} 
@end 

このコードが動作するには、デバイス上でそれをテストしました。

+0

は、あなたの答えをありがとうご清聴ありがとうございましたが、私は「didFinishPickingMediaWithInfoは」仕事にそのために – paris

+0

が動作していないことをお願いしたいと思います、あなたのViewControllerは 'プロトコルを採用/準拠していなければなりません次に、あなたのViewControllerに、そのメソッドの実装を提供します。また、のViewControllerとしてimagePickerの 'delegate'を設定UIImagePickerControllerDelegate'は、すなわち' self' – Prashant

+0

はい、デリゲートは、カメラが動作することができ、されていますが、フォトアルバムは – paris

関連する問題