IOS-UIImagePickerController加载图片

1 首先我们需要绘制故事板中的一些控件,再关联方法,如下图:

绘制相册

2 代码实现:

#import "ViewController.h"

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
}
//相册
- (IBAction)selectImage:(id)sender {
    
    //相册的资源访问UIImagePickerController来读取
    UIImagePickerController *imagePickerC = [[UIImagePickerController alloc]init];
    /*
     UIImagePickerControllerSourceTypePhotoLibrary 相册
     UIImagePickerControllerSourceTypeCamera 拍照 摄像
     UIImagePickerControllerSourceTypeSavedPhotosAlbum 时刻
     */
    //数据源类型
    imagePickerC.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
    //是否允许编辑
    imagePickerC.allowsEditing = YES;
    
    imagePickerC.delegate = self;
    
    [self presentViewController:imagePickerC animated:YES completion:NULL];
    
}
//照相
- (IBAction)takePhoto:(id)sender {
    
    /*
     UIImagePickerControllerCameraDeviceRear:后置摄像头
     UIImagePickerControllerCameraDeviceFront:前置摄像头
     */
    //判断是否有摄像头
    BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
    
    if (!isCamera) {
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"没有可用的摄像头" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertC addAction:action];
        
        [self presentViewController:alertC animated:YES completion:NULL];
        
        return;
    }
    
     UIImagePickerController *imagePickerC = [[UIImagePickerController alloc]init];
    //数据源的类型
    imagePickerC.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    imagePickerC.delegate = self;
    
    [self presentViewController:imagePickerC animated:YES completion:NULL];
    
    
    
}
//视频
- (IBAction)video:(id)sender {
}
//拍摄
- (IBAction)shoot:(id)sender {
}


#pragma mark - UIImagePickerController Delegate
//完成选择之后
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    NSLog(@"info = %@",info);

    /*
     UIImagePickerControllerEditedImage:编辑后的图片
     UIImagePickerControllerOriginalImage:编辑前的图片
     */
    UIImage *img = info[UIImagePickerControllerEditedImage];
    
    self.myImageView.image = img;
    
    //如果是照相
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        
        //保存到相册中
        UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

        
    }
    
    
    [picker dismissViewControllerAnimated:YES completion:NULL];
    
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    NSLog(@"取消选择");
    
    [self dismissViewControllerAnimated:YES completion:NULL];
    
}

//照片保存成功的回调方法  注意:方法名命名有要求
- (void)               image: (UIImage *) image
    didFinishSavingWithError: (NSError *) error
                 contextInfo: (void *) contextInfo{

    NSLog(@"保存成功");
}

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

@end

运行效果:
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,612评论 25 708
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,554评论 0 17
  • 之前的文章提到关于《狐狸和刺猬》(收录于以赛亚•柏林​《俄国思想家》)。对于柏林的这本《俄国思想家》我没读过,所以...
    颠默阅读 2,562评论 0 0
  • 今天在文摘中读了一篇文章《 绊倒你的冰》 冰面光滑,没有阻力,走在上面却极易摔倒,所以绊倒你的不只有逆境,还有顺境...
    有理想的猪阅读 470评论 0 0
  • 这份商场促销员工作,我做了五年半了。 从最初的品牌不被看好,销量惨淡,每天低眉顺眼的拉拢每一位来到货架前的顾客,到...
    捱过我的梦魇阅读 633评论 0 1