美文网首页
iOS 选择图片库

iOS 选择图片库

作者: CaptainRoy | 来源:发表于2018-10-08 17:55 被阅读30次
    懒加载
    #pragma mark - lazy load
    -(UIImageView *)imageView
    {
        if (!_imageView) {
            _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 100.0f, 100.0f)];
            _imageView.backgroundColor = [UIColor redColor];
            _imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClick)];
            [_imageView addGestureRecognizer:tap];
            [self.view addSubview:_imageView];
        }
        return _imageView;
    }
    
    -(UIImagePickerController *)imagePickerController
    {
        if (!_imagePickerController) {
            _imagePickerController = [[UIImagePickerController alloc] init];
            _imagePickerController.delegate = self;
            _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            _imagePickerController.allowsEditing = YES;
        }
        return _imagePickerController;
    }
    
    遵循代理<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
    #pragma mark - click
    -(void)imageClick
    {
        [self presentViewController:self.imagePickerController animated:YES completion:NULL];
    }
    
    
    #pragma mark - UIImagePickerControllerDelegate
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info
    {
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        self.imageView.image = image;
        NSLog(@" %@ ",info);
        [self dismissViewControllerAnimated:YES completion:NULL];
    }
    

    相关文章

      网友评论

          本文标题:iOS 选择图片库

          本文链接:https://www.haomeiwen.com/subject/ccvjaftx.html