iOS - 獲取系統相冊照片名稱,路徑以及各項信息

最近做的項目 , 在我看來都停偏的 , 因爲需要的都不僅僅是展示和業務邏輯 , 而主要都是網絡和存儲,文件,流媒體操作方面的東西 . 所以今天想要獲取下照片的名稱 , 還找了挺久的 . 以此記錄下 , 直接上代碼 .


首先讓當前控制器遵循 UINavigationControllerDelegate,UIImagePickerControllerDelegate 這兩個協議 , 並實現其方法 , 用於打開相冊並選擇照片

//打開相冊
- (void)uploadPhoto
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        //設置當前控制器爲picker對象的代理
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:nil];
    }
}

代理方法

//獲取照片信息
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{

    //獲取圖片的名稱
    //獲取
    NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];

    //導入 #import <AssetsLibrary/AssetsLibrary.h> 庫
    //創建 ALAssetsLibrary 對象
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];

    [library assetForURL:url resultBlock:^(ALAsset *asset){


        //獲取圖片
        UIImage *image = info[UIImagePickerControllerOriginalImage];
        //獲取照片名稱
        NSString *fileName = asset.defaultRepresentation.filename;
        //獲取照片元數據  ,包含一些RGB什麼的
        NSString *fileName = asset.defaultRepresentation.metadata;

       //獲取照片大小比例等等... defaultRepresentation的屬性中可以查看
        ....

        //開始上傳 (此方法無視 , 這是項目中需要的方法)
        [JYCenterAddUploadManager executeUpLoadFileWithFile:image fileParent:@"root" fileName:fileName fileType:JYUpLoadFileTypeImage response:^(JYDownLoadTaskModel *taskModel) {

        }];

    }failureBlock:^(NSError *error){

        [NSObject alertShowWithSingleTipWithtarget:self title:@"獲取相冊失敗" makeSureClick:nil];

    }];

    [picker dismissViewControllerAnimated:YES completion:nil];
}

尤其需要注意的是: 蘋果爲了保護用戶隱私 , 我們開發者是拿不到相冊裏視頻和照片的路徑的 , 此路徑是指物理路徑 , 而並非上述的URL

發佈了50 篇原創文章 · 獲贊 3 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章