获取从UIImagePickerController中选择的原始视频名称

时间:2016-05-23 05:56:31

标签: ios objective-c iphone uiimagepickercontroller

如何在UIImagePickerController的委托方法中获取从相机胶卷或任何其他相册中选择的视频文件的名称?

我可以获取图片的名称,但如果在视频中使用相同图片则会返回nil

3 个答案:

答案 0 :(得分:0)

     - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

            if (![mediaType isEqualToString:(NSString *)kUTTypeMovie])
                return;

            mediaURl = [info objectForKey:UIImagePickerControllerMediaURL];

            //NSLog(@"mediaURL %@",mediaURl);
            moviePath = mediaURl.absoluteString;
            //  NSLog(@"moviePath %@",moviePath);
            tempFilePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
            NSLog(@"filepath %@",tempFilePath);
            //if you want only file name
            NSArray *ar = [tempFilePath componentsSeparatedByString:@"/"];
            NSString *filename = [[ar lastObject] uppercaseString];
            NSLog(@"filename %@",filename);
    }

如果您有任何问题,请告诉我

答案 1 :(得分:0)

我知道这个线程非常老,但是我想如果有人找到它,这是一个迅速可行的答案:

func fileName(for infoDict: [String : Any]) -> String? {
    guard let referenceURL = infoDict[UIImagePickerControllerReferenceURL] as? URL else { return nil }
    let result = PHAsset.fetchAssets(withALAssetURLs: [referenceURL], options: nil)
    guard let asset = result.firstObject else { return nil }
    let firstResource = PHAssetResource.assetResources(for: asset).first
    return firstResource?.originalFilename ?? asset.value(forKey: "filename") as? String
}

答案 2 :(得分:0)

Swift 4或更高版本 在我的身边为捕获的图像工作

使用:

    imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){
            let fileName = FileInfo.getMediaName(info: info)
      }

class:请放置方法 info [:]

class FileInfo {
    // Return Media name
    static func getMediaName(info:[String : Any]) -> String {

        if let asset = info[UIImagePickerControllerPHAsset] as? PHAsset {
            let assetResources = PHAssetResource.assetResources(for: asset)
            let firstObj = assetResources.first?.originalFilename as! NSString
            print(firstObj)
            return firstObj
        }
    }
}

// IMG_02256.jpeg