UIImagePickerControllerReferenceURL always returns nill

后端 未结 3 1647
孤城傲影
孤城傲影 2021-01-05 13:02

I am trying to get the name of the image which I have just captured from camera with following code. But [info objectForKey:@\"UIImagePickerControllerReferenceURL\"]

相关标签:
3条回答
  • 2021-01-05 13:18

    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.

    0 讨论(0)
  • 2021-01-05 13:34

    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);
            });
        });
    }
    
    0 讨论(0)
  • 2021-01-05 13:38

    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.

    0 讨论(0)
提交回复
热议问题