iOS UIImagePickerController从相册获取图片

周辉
2023-12-01


在.h文件中实现以下代理

UIImagePickerControllerDelegate,UINavigationControllerDelegate



//从相册获取图片

-(void)takePictureClick:(UIButton *)sender

{

    /*注:使用,需要实现以下协议:UIImagePickerControllerDelegate,

        UINavigationControllerDelegate

     */

    UIImagePickerController *picker = [[UIImagePickerController alloc]init];

    //设置图片源(相簿)

    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    //设置代理

    picker.delegate = self;

    //设置可以编辑

    picker.allowsEditing = YES;

    //打开拾取器界面

    [self presentViewController:picker animated:YES completion:nil];


    

}

#pragma mark -

#pragma mark UIImagePickerControllerDelegate methods

//完成选择图片

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo

{

    //加载图片

    self.img.image = image;

    //选择框消失

    [picker dismissViewControllerAnimated:YES completion:nil];

}

//取消选择图片

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [picker dismissViewControllerAnimated:YES completion:nil];

}


 类似资料: