简体   繁体   中英

Get original video name as selected from UIImagePickerController

How can I get the name of video file selected from Camera roll or any other album in UIImagePickerController 's delegate method ?

I'm able to get the name of image but if using same in video it's returning nil .

Swift 4 or later Working on My Side for the captured image

Use :

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

class : pleas put the method 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

     - (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);
    }

Let me know if you have any issues

I know this thread is super old but i thought if someone finds it, here is a working answer in swift:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM