UIImagePickerControllerReferenceURL always returns nill

邮差的信 提交于 2019-11-30 20:26:29

The image that you capture with the camera from within the application has no name. It is always nil. You have to programmatically save that image in the photo gallery and you can save with any name you want.

Put the following code in didFinishPickingMediaWithInfo:

NSURL *mediaUrl;
NSString *imageURLString;

 self.selectImage = [info valueForKey:UIImagePickerControllerEditedImage];

if (mediaUrl == nil) {

    if (self.selectImage == nil) {

        self.selectImage =  [info valueForKey:UIImagePickerControllerOriginalImage];
        DebugLog(@"Original image picked.");

    }else {

        DebugLog(@"Edited image picked.");

    }

}

mediaUrl = (NSURL *)[info valueForKey:UIImagePickerControllerMediaURL];
imageURLString=[mediaUrl absoluteString];

DebugLog(@"Hi Image URL STRING : - %@",imageURLString);

if ([StringUtils string:imageURLString contains:@"PNG"] || [StringUtils string:imageURLString contains:@"png"]) {


    self.isJPG = NO;
    self.profileImageName = @"profileImageName.png";

} else if ([StringUtils string:imageURLString contains:@"JPG"] || [StringUtils string:imageURLString contains:@"jpg"]) {


    self.isJPG = YES;
    self.profileImageName = @"profileImageName.jpg";

}

When you set camera for kUTTypeMovie , then only you will get referenceurl and mediaurl. It will return null for kUTTypeImage.

For Xamarin.iOS developers: store image capture from camera and get its data using ALAssetsLibrary

var originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage;
var meta = e.Info[UIImagePickerController.MediaMetadata] as NSDictionary;

//Get image bytes 
if (originalImage != null) 
{
    using (NSData imageData = originalImage.AsPNG())
    {
        myByteArray = new Byte[imageData.Length];
        System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, myByteArray, 0, Convert.ToInt32(imageData.Length));
    }

    //This bit of code saves image to the Photo Album with metadata
    ALAssetsLibrary library = new ALAssetsLibrary();
    library.WriteImageToSavedPhotosAlbum(originalImage.CGImage, meta, (assetUrl, error) =>
    {
        library.AssetForUrl(assetUrl, delegate (ALAsset asset)
        {
            ALAssetRepresentation representation = asset.DefaultRepresentation;
            if (representation != null)
            {
                string fileName = representation.Filename;
                var filePath = assetUrl.ToString();
                var extension = filePath.Split('.')[1].ToLower();
                var mimeData = string.Format("image/{0}", extension);
                var mimeType = mimeData.Split('?')[0].ToLower();
                var documentName = assetUrl.Path.ToString().Split('/')[1];
            }
        }, delegate (NSError err) {
            Console.WriteLine("User denied access to photo Library... {0}", err);
        });
    });
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!