iOS弹出系统相册选择弹窗

直接上代码

复制代码
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self; //设置代理
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [navigationController presentViewController:imagePickerController animated:YES completion:nil];
    }
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:^{}];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; //通过key值获取到图片
    self.avatarView.image = image;
}

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

注意,弹出相册选择弹窗,不需要申请相册权限, 只有将图片写入系统相册

才需要这个权限,并且,弹出系统相册弹窗的时候,要添加这个判断

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

相关推荐
谈吐大方的鹏sir1 小时前
SwiftUI-TextField组件学习
ios
谈吐大方的鹏sir4 小时前
SwiftUI-Button组件学习
ios
谈吐大方的鹏sir6 小时前
SwiftUI-Image组件学习
ios
鹏多多.7 小时前
flutter-完美解决键盘弹出遮挡输入框的问题
android·flutter·ios·前端框架
杂雾无尘9 小时前
SwiftUI 动画新技能,让你的应用「活」起来!
ios·swiftui·swift
林大鹏天地10 小时前
iOS18系统 [YYKVStorage _dbClose] 偶现崩溃
ios
林大鹏天地10 小时前
使用Xcode16打包后,App在暗夜模式下,iOS18 切换Tabbar的item,会有一根白色线条闪过。
ios
2501_9159090610 小时前
iOS 加固工具实战解析,主流平台审核机制与工具应对策略
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063212 小时前
iOS WebView 调试实战,第三方脚本加载失败与内容安全策略冲突问题排查指南
android·ios·小程序·https·uni-app·iphone·webview
笑尘pyrotechnic1 天前
DocC的简单使用
ios·objective-c