예제 #1
0
        private MediaResult CreateResult(PickerOperation operation, NSDictionary <NSString, iOSObjectProxy> info)
        {
            if (info == null)
            {
                return(null);
            }

            switch (Operation)
            {
            case PickerOperation.PickGallery:
                // User can either pick an image or a video
                NSURL pickedURL = null;
                if (TryGetUrl(info, UIImagePickerController.UIImagePickerControllerImageURL, out pickedURL))
                {
                    return(new MediaResult(MediaType.Image, null, pickedURL.AbsoluteString.UTF8String));
                }
                else if (TryGetUrl(info, UIImagePickerController.UIImagePickerControllerMediaURL, out pickedURL))
                {
                    return(new MediaResult(MediaType.Video, null, pickedURL.AbsoluteString.UTF8String));
                }
                else
                {
                    return(null);
                }

            case PickerOperation.TakePicture:
                // Get the newly taken image, save it into the into user temporary folder and return the URL.
                // The image name will be "IMG_" + timestamp and format is JPG.
                UIImage picture = (UIImage)info.ValueForKey(UIImagePickerController.UIImagePickerControllerOriginalImage,
                                                            ptr => PInvokeUtil.IsNull(ptr) ? null : new UIImage(ptr));

                if (picture == null)
                {
                    Debug.LogError("Couldn't get the taken picture.");
                    return(null);
                }

                // Save the image into user temporary folder and return the URL.
                NSString tempDir = NSFileManager.NSTemporaryDirectory();

                if (tempDir == null)
                {
                    Debug.LogError("Couldn't find the path of user's temporary directory.");
                    return(null);
                }

                // The image name is "IMG_" + timestamp and format is JPG.
                NSString pictureName = NSString.StringWithUTF8String("IMG_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg");
                NSURL    pictureURL  = NSURL.FileURLWithPath(tempDir).URLByAppendingPathComponent(pictureName);

                if (pictureURL != null)
                {
                    NSData imageData = UIFunctions.UIImageJPEGRepresentation(picture, 1f);
                    NSFileManager.DefaultManager.CreateFileAtPath(pictureURL.Path, imageData, null);
                    return(new MediaResult(MediaType.Image, null, pictureURL.AbsoluteString.UTF8String));
                }
                else
                {
                    return(null);
                }

            case PickerOperation.RecordVideo:
                NSURL videoURL = null;
                if (TryGetUrl(info, UIImagePickerController.UIImagePickerControllerMediaURL, out videoURL))
                {
                    return(new MediaResult(MediaType.Video, null, videoURL.AbsoluteString.UTF8String));
                }
                else
                {
                    return(null);
                }

            default:
                return(null);
            }
        }
예제 #2
0
 internal InternalUIImagePickerControllerDelegate(PickerOperation op)
 {
     Operation = op;
 }