-(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];
}
但是在相册选择的照片时只会执行取消但是完成不执行,试过这个项目随处写的都不行,我再打开一个项目可以去代理,一个伟大的上帝保存让我目瞪口呆..
答案 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
此代码可以在设备上进行测试。