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) {

相关推荐
壹方秘境3 天前
我用Go语言开发了一个跨平台的HTTPS抓包和调试工具
前端·后端·ios
初级代码游戏9 天前
easy Photo Clean公测版:快速清理iPhone照片 邀请公测
ios·iphone
库奇噜啦呼9 天前
【iOS】RunLoop学习
学习·ios
黑科技iOS上架9 天前
iOS应用周末提交什么情况算卡审
经验分享·ios
zzb15809 天前
ios基础-MVC-UIView
ios·mvc·cocoa
kingbal9 天前
Flutter:Flutter SDK版本管理工具FVM
android·flutter·ios·android-studio·window
他们都不看好你,偏偏你最不争气9 天前
【iOS】Runtime - Part 2 && 消息发送:缓存、查找与转发
macos·ios·objective-c·cocoa
2501_9159184110 天前
iOS App性能测试工具的实现方法与优化循环指南
android·ios·小程序·https·uni-app·iphone·webview
他们都不看好你,偏偏你最不争气10 天前
【iOS】Runtime - Part 1 && 对象与类的本质
macos·ios·objective-c·cocoa