1

I am trying (AND FAILING) to get the user to upload an image and then pass the image name and actual image to another controller. This worked fine on a simulator and an actual device before ios11. But now, it just works on the simulator and crashes every time on the actual device. I am using TestFlight to test this so I am unable to see the errors on the device. But I saw this and was able to create my method which looks like this:

@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    if #available(iOS 9.0, *) {
        let url = info[UIImagePickerControllerReferenceURL] as! URL
        //let assets = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil)
        //imageName = PHAssetResource.assetResources(for: assets.firstObject!).first!.originalFilename
        let result = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil)
        imageName = PHAssetResource.assetResources(for: result.firstObject!).first!.originalFilename
//            let asset = result.firstObject
//            if(asset == nil){
//                print("asset is NIL")
//            }else {
//                print("asset is not NIL")
//            }
//            print(asset?.value(forKey: "filename"))
//            iconImageName = asset?.value(forKey: "filename") as! String
        print("FILENAME START")
        print(iconImageName)
        print("FILENAME END")
        } else {
        // Fallback on earlier versions
    }

    self.dismiss(animated: true, completion: { () -> Void in
    })
}

the commented out code are other ways I tried to get the file name. Why does this work on a simulator but not on a real device? I have looked online but this seems like the right way except it is not. PS: Long story but my device does not work when connected to my Mac, which is why I am using TestFlight.

Aria
  • 389
  • 3
  • 7
  • 25
  • Use Xcode and debug this on a device with iOS 11 so you can get all details about the error. – rmaddy Feb 07 '18 at 15:57
  • @rmaddy I can't do that at the moment. Long story but my device does not work when connected to my Mac, which is why I am using TestFlight, do you have any other suggestions? – Aria Feb 07 '18 at 16:00

3 Answers3

1

You can get fileName from UIImagePicker easily by this way:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let imageURL = info[UIImagePickerControllerReferenceURL] as? URL {
        let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
        let asset = result.firstObject
        print(asset?.value(forKey: "filename"))

    }

    dismiss(animated: true, completion: nil)
}

For further information, you can follow this answer: https://stackoverflow.com/a/40628457/5167909

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
0

I believe you can get the image path(and from there the filename easily) from the info dictionary: info[UIImagePickerControllerImageURL]

pera
  • 155
  • 4
-1

Try this:

PHAsset *asset = [self assetFromDictionary:info];
if (asset) {
    NSData *fileData = nil;
    NSString *fileName = [self filenameForAsset:asset];
}

- (PHAsset *)assetFromDictionary:(NSDictionary *)info {
    PHAsset *asset = nil;

    if (@available(iOS 11.0, *)) {
        NSURL *assetURL = info[UIImagePickerControllerImageURL];
        if (assetURL) {
            asset = [info valueForKey:UIImagePickerControllerPHAsset];
        }
    } else {
        NSURL *assetURL = info[UIImagePickerControllerReferenceURL];
        if (assetURL) {
            asset = [[PHAsset fetchAssetsWithALAssetURLs:@[assetURL] options:nil] lastObject];
        }
    }

    return asset;
}

- (NSString *)filenameForAsset:(PHAsset *)asset {
    NSString *filename = @"";
    if (@available(iOS 9.0, *)) {
        filename = [[PHAssetResource assetResourcesForAsset:asset] firstObject].originalFilename;
    } else {
        filename = [asset valueForKey:@"filename"];
    }

    return filename;
}
landonandrey
  • 1,271
  • 1
  • 16
  • 26