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

相关推荐
牛马11110 小时前
iOS :Codable协议,字典,数组的详细分析和比较
ios
TheNextByte115 小时前
如何将 iPhone 备份到外置硬盘?
ios·iphone
佛系打工仔15 小时前
绘制K线第三章:拖拽功能实现
android·前端·ios
游戏开发爱好者819 小时前
2025年iOS应用上架App Store全指南,开发者必看
android·ios·小程序·https·uni-app·iphone·webview
HH思️️无邪20 小时前
Flutter 项目 -从 0 到 1 实现 iOS WatchApp
flutter·ios
TheNextByte121 小时前
如何将 iPhone 上的联系人备份到 iCloud?
ios·iphone·icloud
牛马11121 小时前
iOS swift 自定义View
ios·cocoa·swift
2501_915106321 天前
HBuilderX 项目上架 iOS app上架 App Store 的关键流程
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
iOS 文件管理,在不越狱的前提下管理 iPhone / iPad 文件
android·ios·小程序·uni-app·iphone·webview·ipad
牛马1111 天前
ios swift处理json数据
ios·json·swift