private async Task<NSUrl> SavePhotoWithLocationAsync(NSDictionary info) { var image = (UIImage)info[UIImagePickerController.EditedImage]; if (image == null) image = (UIImage)info[UIImagePickerController.OriginalImage]; var metadata = info[UIImagePickerController.MediaMetadata] as NSDictionary; var newMetadata = new NSMutableDictionary(metadata); if (!newMetadata.ContainsKey(ImageIO.CGImageProperties.GPSDictionary)) { var gpsData = await BuildGPSDataAsync(); if (gpsData != null) newMetadata.Add(ImageIO.CGImageProperties.GPSDictionary, gpsData); } // save to camera roll with metadata var assetUrlTCS = new TaskCompletionSource<NSUrl>(); using (var library = new ALAssetsLibrary()) { library.WriteImageToSavedPhotosAlbum(image.CGImage, newMetadata, (newAssetUrl, error) => { // any additional processing can go here if (error == null) assetUrlTCS.SetResult(newAssetUrl); else assetUrlTCS.SetException(new Exception(error.LocalizedFailureReason)); }); } return await assetUrlTCS.Task; }
/// <summary> /// Gets the picture media file. /// </summary> /// <param name="info">The information.</param> /// <returns>MediaFile.</returns> private void GetPictureMediaFile(NSDictionary info, Action <MediaFile> callback) { var image = (UIImage)info[UIImagePickerController.OriginalImage]; var referenceURL = (NSUrl)info[UIImagePickerController.ReferenceUrl]; if (referenceURL == null) //from camera { var metadata = (NSDictionary)info[UIImagePickerController.MediaMetadata]; ALAssetsLibrary library = new ALAssetsLibrary(); library.WriteImageToSavedPhotosAlbum(image.CGImage, metadata, (assetUrl, error) => { }); GetPictureMediaFileWithMetadata(metadata, image, callback); } else // from albun { ALAssetsLibrary library = new ALAssetsLibrary(); library.AssetForUrl(referenceURL, (ALAsset asset) => { ALAssetRepresentation rep = asset.DefaultRepresentation; NSDictionary metadata = rep.Metadata; var cgimg = rep.GetFullScreenImage(); var img = new UIImage(cgimg); GetPictureMediaFileWithMetadata(metadata, img, callback); }, (e) => { }); } }
public async Task <Stream> CaptureImage() { var connection = outputSession.Connections[0]; var sampleBuffer = await outputSession.CaptureStillImageTaskAsync(connection); var imageData = AVCaptureStillImageOutput.JpegStillToNSData(sampleBuffer); byte[] dataBytes = new byte[imageData.Length]; System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, dataBytes, 0, Convert.ToInt32(imageData.Length)); var image = CIImage.FromData(imageData); var metadata = image.Properties.Dictionary.MutableCopy() as NSMutableDictionary; ALAssetsLibrary library = new ALAssetsLibrary(); library.WriteImageToSavedPhotosAlbum(imageData, metadata, (assetUrl, error) => { if (error == null) { Console.WriteLine("assetUrl:" + assetUrl); } else { Console.WriteLine(error); } }); return(new MemoryStream(dataBytes)); }
public void SavePictureToLibrary(byte[] imgData) { ALAssetsLibrary library = new ALAssetsLibrary(); library.WriteImageToSavedPhotosAlbum(imgData.ToImage().CGImage, new NSDictionary(), (assetUrl, error) => { Console.WriteLine("assetUrl:" + assetUrl); }); }
public void SaveFileToPhotosAlbum(string filePath, byte[] fileData) { string ext = Path.GetExtension(filePath); if (ext.ToUpper() == ".MOV" || ext.ToUpper() == ".M4V") { File.WriteAllBytes(filePath, fileData); if (UIVideo.IsCompatibleWithSavedPhotosAlbum(filePath)) { UIVideo.SaveToPhotosAlbum(filePath, (path, error) => { if (error == null) { if (FileSavedToPhotosAlbum != null) { FileSavedToPhotosAlbum(this, new FilesSavedToPhotosAlbumArgs(path, path)); } } else { Console.Out.WriteLine("Video {0} cannot be saved to photos album!", filePath); } }); } } else if (ext.ToUpper() == ".JPEG" || ext.ToUpper() == ".JPG" || ext.ToUpper() == ".PNG") { NSData imgData = NSData.FromArray(fileData); var img = UIImage.LoadFromData(imgData); var meta = new NSDictionary(); ALAssetsLibrary library = new ALAssetsLibrary(); library.WriteImageToSavedPhotosAlbum(img.CGImage, meta, (assetUrl, error) => { if (error == null) { if (FileSavedToPhotosAlbum != null) { FileSavedToPhotosAlbum(this, new FilesSavedToPhotosAlbumArgs(assetUrl.ToString(), filePath)); } else { Console.Out.WriteLine("Image {0} cannot be saved to photos album!", filePath); } } }); img.Dispose(); } else { File.WriteAllBytes(filePath, fileData); if (FileSavedToPhotosAlbum != null) { FileSavedToPhotosAlbum(this, new FilesSavedToPhotosAlbumArgs(filePath, filePath)); } } }
public override void ViewDidLoad () { base.ViewDidLoad (); Title = "Save to Album"; View.BackgroundColor = UIColor.White; cameraButton = UIButton.FromType (UIButtonType.RoundedRect); cameraButton.Frame = new CGRect(10, 20, 100,40); cameraButton.SetTitle ("Camera", UIControlState.Normal); cameraButton.TouchUpInside += (sender, e) => { TweetStation.Camera.TakePicture (this, (obj) =>{ // https://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerControllerDelegate_Protocol/UIImagePickerControllerDelegate/UIImagePickerControllerDelegate.html#//apple_ref/occ/intfm/UIImagePickerControllerDelegate/imagePickerController:didFinishPickingMediaWithInfo: var photo = obj.ValueForKey(new NSString("UIImagePickerControllerOriginalImage")) as UIImage; var meta = obj.ValueForKey(new NSString("UIImagePickerControllerMediaMetadata")) as NSDictionary; // This bit of code saves to the Photo Album with metadata ALAssetsLibrary library = new ALAssetsLibrary(); library.WriteImageToSavedPhotosAlbum (photo.CGImage, meta, (assetUrl, error) =>{ Console.WriteLine ("assetUrl:"+assetUrl); }); // This bit of code does basic 'save to photo album', doesn't save metadata // var someImage = UIImage.FromFile("someImage.jpg"); // someImage.SaveToPhotosAlbum ((image, error)=> { // var o = image as UIImage; // Console.WriteLine ("error:" + error); // }); // This bit of code saves to the application's Documents directory, doesn't save metadata // var documentsDirectory = Environment.GetFolderPath // (Environment.SpecialFolder.Personal); // string jpgFilename = System.IO.Path.Combine (documentsDirectory, "Photo.jpg"); // NSData imgData = photo.AsJPEG(); // NSError err = null; // if (imgData.Save(jpgFilename, false, out err)) // { // Console.WriteLine("saved as " + jpgFilename); // } else { // Console.WriteLine("NOT saved as" + jpgFilename + " because" + err.LocalizedDescription); // } }); }; View.Add (cameraButton); if (!UIImagePickerController.IsSourceTypeAvailable (UIImagePickerControllerSourceType.Camera)) { cameraButton.SetTitle ("No camera", UIControlState.Disabled); cameraButton.SetTitleColor (UIColor.Gray, UIControlState.Disabled); cameraButton.Enabled = false; } }
public override void ViewDidLoad() { base.ViewDidLoad(); Title = "Save to Album"; View.BackgroundColor = UIColor.White; cameraButton = UIButton.FromType(UIButtonType.RoundedRect); cameraButton.Frame = new CGRect(10, 20, 100, 40); cameraButton.SetTitle("Camera", UIControlState.Normal); cameraButton.TouchUpInside += (sender, e) => { TweetStation.Camera.TakePicture(this, (obj) => { // https://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerControllerDelegate_Protocol/UIImagePickerControllerDelegate/UIImagePickerControllerDelegate.html#//apple_ref/occ/intfm/UIImagePickerControllerDelegate/imagePickerController:didFinishPickingMediaWithInfo: var photo = obj.ValueForKey(new NSString("UIImagePickerControllerOriginalImage")) as UIImage; var meta = obj.ValueForKey(new NSString("UIImagePickerControllerMediaMetadata")) as NSDictionary; // This bit of code saves to the Photo Album with metadata ALAssetsLibrary library = new ALAssetsLibrary(); library.WriteImageToSavedPhotosAlbum(photo.CGImage, meta, (assetUrl, error) => { Console.WriteLine("assetUrl:" + assetUrl); }); // This bit of code does basic 'save to photo album', doesn't save metadata // var someImage = UIImage.FromFile("someImage.jpg"); // someImage.SaveToPhotosAlbum ((image, error)=> { // var o = image as UIImage; // Console.WriteLine ("error:" + error); // }); // This bit of code saves to the application's Documents directory, doesn't save metadata // var documentsDirectory = Environment.GetFolderPath // (Environment.SpecialFolder.Personal); // string jpgFilename = System.IO.Path.Combine (documentsDirectory, "Photo.jpg"); // NSData imgData = photo.AsJPEG(); // NSError err = null; // if (imgData.Save(jpgFilename, false, out err)) // { // Console.WriteLine("saved as " + jpgFilename); // } else { // Console.WriteLine("NOT saved as" + jpgFilename + " because" + err.LocalizedDescription); // } }); }; View.Add(cameraButton); if (!UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) { cameraButton.SetTitle("No camera", UIControlState.Disabled); cameraButton.SetTitleColor(UIColor.Gray, UIControlState.Disabled); cameraButton.Enabled = false; } }
public void SaveImageToLibrary(string fileName, byte[] imageData) { try { var nsData = NSData.FromArray(imageData); var uiImage = UIImage.LoadFromData(nsData); var library = new ALAssetsLibrary(); library.WriteImageToSavedPhotosAlbum(uiImage.CGImage, new NSDictionary(), (assetUrl, error) => { Console.WriteLine("assetUrl:" + assetUrl); }); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public void DownloadHomepage() { var webClient = new WebClient(); webClient.DownloadStringCompleted += (sender, e) => { if (e.Cancelled || e.Error != null) { // do something with error } string contents = e.Result; int length = contents.Length; InvokeOnMainThread(() => { ResultTextView.Text += "Downloaded the html and found out the length.\n\n"; }); webClient.DownloadDataCompleted += (sender1, e1) => { if (e1.Cancelled || e1.Error != null) { // do something with error } SaveBytesToFile(e1.Result, "team.jpg"); InvokeOnMainThread(() => { ResultTextView.Text += "Downloaded the image.\n"; DownloadedImageView.Image = UIImage.FromFile(localPath); }); ALAssetsLibrary library = new ALAssetsLibrary(); var dict = new NSDictionary(); library.WriteImageToSavedPhotosAlbum(DownloadedImageView.Image.CGImage, dict, (s2, e2) => { InvokeOnMainThread(() => { ResultTextView.Text += "Saved to album assetUrl\n"; }); if (downloaded != null) { downloaded(length); } }); }; webClient.DownloadDataAsync(new Uri("http://xamarin.com/images/about/team.jpg")); }; webClient.DownloadStringAsync(new Uri("http://xamarin.com/")); }
/* * Setting the connection to be from camera feed and capture the image * jpegAsByteArray is used to save image pixel values to send to resultViewController to calculate concentration of sample * Asynchoronouse task needs async */ public async Task takeThepictureAsync() { var videoConnection = stillImageOutput.ConnectionFromMediaType(AVMediaType.Video); var sampleBuffer = await stillImageOutput.CaptureStillImageTaskAsync(videoConnection); var jpegImageAsNsData = AVCaptureStillImageOutput.JpegStillToNSData(sampleBuffer); jpegAsByteArray = jpegImageAsNsData; var imageMeta = CIImage.FromData(jpegImageAsNsData); // creating an image and putting it in ImagePreview frame on top of the screen var imagePrev = new UIImage(jpegImageAsNsData); //UIImage.FromImage(imageMeta); ImagePreview.Image = imagePrev; sampleBuffer.Dispose(); // Saving the complete image into the phone gallery with addition to its meta data var meta = imageMeta.Properties.Dictionary.MutableCopy() as NSMutableDictionary; var library = new ALAssetsLibrary(); library.WriteImageToSavedPhotosAlbum(jpegImageAsNsData, meta, (assetUrl, error) => { if (error == null) { Console.WriteLine("saved: ");//+jpegImageAsNsData); } else { Console.WriteLine(error); UIAlertView alert = new UIAlertView() { Title = "Alert!", Message = "There was a problem with saving your image, please take a new picture" }; alert.AddButton("OK"); alert.Show(); } }); }
private async Task <NSUrl> SavePhotoWithLocationAsync(NSDictionary info) { var image = (UIImage)info[UIImagePickerController.EditedImage]; if (image == null) { image = (UIImage)info[UIImagePickerController.OriginalImage]; } var metadata = info[UIImagePickerController.MediaMetadata] as NSDictionary; var newMetadata = new NSMutableDictionary(metadata); if (!newMetadata.ContainsKey(ImageIO.CGImageProperties.GPSDictionary)) { var gpsData = await BuildGPSDataAsync(); if (gpsData != null) { newMetadata.Add(ImageIO.CGImageProperties.GPSDictionary, gpsData); } } // save to camera roll with metadata var assetUrlTCS = new TaskCompletionSource <NSUrl>(); using (var library = new ALAssetsLibrary()) { library.WriteImageToSavedPhotosAlbum(image.CGImage, newMetadata, (newAssetUrl, error) => { // any additional processing can go here if (error == null) { assetUrlTCS.SetResult(newAssetUrl); } else { assetUrlTCS.SetException(new Exception(error.LocalizedFailureReason)); } }); } return(await assetUrlTCS.Task); }
public string GetImagePath(Object CurrentObject) { //String filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "Library", "MyData.db"); //return filename; var tempcontroller = new UIViewController(); Camera.TakePicture(tempcontroller, (obj) => { var photo = obj.ValueForKey(new NSString("UIImagePickerControllerOriginalImage")) as UIImage; var meta = obj.ValueForKey(new NSString("UIImagePickerControllerMediaMetadata")) as NSDictionary; ALAssetsLibrary library = new ALAssetsLibrary(); library.WriteImageToSavedPhotosAlbum(photo.CGImage, meta, (assetUrl, error) => { Console.WriteLine("assetUrl:" + assetUrl); }); });; return("path"); }
public void DownloadHomepage() { var webClient = new WebClient(); webClient.DownloadStringCompleted += (sender, e) => { if(e.Cancelled || e.Error != null) { // do something with error } string contents = e.Result; int length = contents.Length; InvokeOnMainThread (() => { ResultTextView.Text += "Downloaded the html and found out the length.\n\n"; }); webClient.DownloadDataCompleted += (sender1, e1) => { if(e1.Cancelled || e1.Error != null) { // do something with error } SaveBytesToFile(e1.Result, "team.jpg"); InvokeOnMainThread (() => { ResultTextView.Text += "Downloaded the image.\n"; DownloadedImageView.Image = UIImage.FromFile (localPath); }); ALAssetsLibrary library = new ALAssetsLibrary(); var dict = new NSDictionary(); library.WriteImageToSavedPhotosAlbum (DownloadedImageView.Image.CGImage, dict, (s2,e2) => { InvokeOnMainThread (() => { ResultTextView.Text += "Saved to album assetUrl\n"; }); if (downloaded != null) downloaded(length); }); }; webClient.DownloadDataAsync(new Uri("http://xamarin.com/images/about/team.jpg")); }; webClient.DownloadStringAsync(new Uri("http://xamarin.com/")); }
public override void ViewDidLoad() { base.ViewDidLoad(); Title = "Color Controls Pro"; View.BackgroundColor = UIColor.White; cameraButton = UIButton.FromType(UIButtonType.RoundedRect); cameraButton.Frame = new CGRect(10, 60, 90, 40); cameraButton.SetTitle("Camera", UIControlState.Normal); cameraButton.TouchUpInside += (sender, e) => { TweetStation.Camera.TakePicture(this, (obj) => { // https://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerControllerDelegate_Protocol/UIImagePickerControllerDelegate/UIImagePickerControllerDelegate.html#//apple_ref/occ/intfm/UIImagePickerControllerDelegate/imagePickerController:didFinishPickingMediaWithInfo: var photo = obj.ValueForKey(new NSString("UIImagePickerControllerOriginalImage")) as UIImage; var meta = obj.ValueForKey(new NSString("UIImagePickerControllerMediaMetadata")) as NSDictionary; sourceImage = photo; displayImage = sourceImage.Scale(new CGSize(300, 200)); imageView.Image = displayImage; sourceMeta = meta; }); }; View.Add(cameraButton); resetButton = UIButton.FromType(UIButtonType.RoundedRect); resetButton.Frame = new CGRect(110, 60, 90, 40); resetButton.SetTitle("Reset", UIControlState.Normal); resetButton.TouchUpInside += (sender, e) => { sliderS.Value = 1; sliderB.Value = 0; sliderC.Value = 1; HandleValueChanged(sender, e); }; View.Add(resetButton); saveButton = UIButton.FromType(UIButtonType.RoundedRect); saveButton.Frame = new CGRect(210, 60, 90, 40); saveButton.SetTitle("Save", UIControlState.Normal); saveButton.TouchUpInside += (sender, e) => { ALAssetsLibrary library = new ALAssetsLibrary(); var img = AdjustImage(sourceImage); if (sourceMeta == null) { sourceMeta = new NSDictionary(); // when using 'clouds.jpg' } library.WriteImageToSavedPhotosAlbum(img.CGImage, sourceMeta, (assetUrl, error) => { Console.WriteLine("SAVED TO assetUrl:" + assetUrl); new UIAlertView("Saved", "Photo saved to Camera Roll", null, "OK", null).Show(); }); }; View.Add(saveButton); labelC = new UILabel(new CGRect(10, 120, 90, 20)); labelS = new UILabel(new CGRect(10, 160, 90, 20)); labelB = new UILabel(new CGRect(10, 200, 90, 20)); labelC.Text = "Contrast"; labelS.Text = "Saturation"; labelB.Text = "Brightness"; View.Add(labelC); View.Add(labelS); View.Add(labelB); sliderB = new UISlider(new CGRect(100, 120, 210, 20)); sliderS = new UISlider(new CGRect(100, 160, 210, 20)); sliderC = new UISlider(new CGRect(100, 200, 210, 20)); // http://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CoreImageFilterReference/Reference/reference.html#//apple_ref/doc/filter/ci/CIColorControls sliderS.MinValue = 0; sliderS.MaxValue = 2; sliderS.Value = 1; sliderB.MinValue = -1; sliderB.MaxValue = 1; sliderB.Value = 0; sliderC.MinValue = 0; sliderC.MaxValue = 4; sliderC.Value = 1; sliderC.TouchUpInside += HandleValueChanged; sliderS.TouchUpInside += HandleValueChanged; sliderB.TouchUpInside += HandleValueChanged; View.Add(sliderC); View.Add(sliderS); View.Add(sliderB); imageView = new UIImageView(new CGRect(10, 240, 300, 200)); sourceImage = UIImage.FromFile("clouds.jpg"); displayImage = sourceImage; imageView.Image = displayImage; View.Add(imageView); if (!UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) { cameraButton.SetTitle("No camera", UIControlState.Disabled); cameraButton.SetTitleColor(UIColor.Gray, UIControlState.Disabled); cameraButton.Enabled = false; } }
public void SaveFileToPhotosAlbum(string filePath, byte[] fileData) { string ext = Path.GetExtension (filePath); if (ext.ToUpper () == ".MOV" || ext.ToUpper () == ".M4V") { File.WriteAllBytes (filePath, fileData); if (UIVideo.IsCompatibleWithSavedPhotosAlbum(filePath)) { UIVideo.SaveToPhotosAlbum(filePath, (path, error) => { if (error == null) { if (FileSavedToPhotosAlbum != null) { FileSavedToPhotosAlbum (this, new FilesSavedToPhotosAlbumArgs (path, path)); } } else { Console.Out.WriteLine ("Video {0} cannot be saved to photos album!", filePath); } }); } } else if (ext.ToUpper () == ".JPEG" || ext.ToUpper () == ".JPG" || ext.ToUpper () == ".PNG") { NSData imgData = NSData.FromArray(fileData); var img = UIImage.LoadFromData(imgData); var meta = new NSDictionary(); ALAssetsLibrary library = new ALAssetsLibrary(); library.WriteImageToSavedPhotosAlbum (img.CGImage, meta, (assetUrl, error) => { if (error == null) { if (FileSavedToPhotosAlbum != null) { FileSavedToPhotosAlbum (this, new FilesSavedToPhotosAlbumArgs (assetUrl.ToString(), filePath)); } else { Console.Out.WriteLine ("Image {0} cannot be saved to photos album!", filePath); } } }); img.Dispose(); } else { File.WriteAllBytes (filePath, fileData); if (FileSavedToPhotosAlbum != null) { FileSavedToPhotosAlbum (this, new FilesSavedToPhotosAlbumArgs (filePath, filePath)); } } }
public void CapturePicture() { try { var parent = UIApplication.SharedApplication.KeyWindow.RootViewController; var imagePicker = new UIImagePickerController { SourceType = UIImagePickerControllerSourceType.Camera }; parent.PresentViewController(imagePicker, true, null); imagePicker.TakePicture(); //(this, (obj)=> //{ //}); imagePicker.FinishedPickingMedia += (sender, e) => { var filepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "tmp.png"); var image = (UIImage)e.Info.ObjectForKey(new NSString("UIImagePickerControllerOriginalImage")); #region image.AsPNG().Save(filepath, false); var path = filepath; //tco.ShowImage(path); #endregion #region var metadata = (NSDictionary)e.Info.ObjectForKey(new NSString("UIImagePickerControllerMediaMetadata")); ALAssetsLibrary library = new ALAssetsLibrary(); library.WriteImageToSavedPhotosAlbum(image.CGImage, metadata, (NSUrl arg1, NSError arg2) => { var paths = arg1.ToString(); }); //library.WriteImageToSavedPhotosAlbum(photo.CGImage, meta, (assetUrl, error) #endregion /*#region * var imagetoalbum = UIImage.FromFile(path); * imagetoalbum.SaveToPhotosAlbum((UIImage image, NSError error) => * { * var o = image; * }); #endregion*/ //tco.ShowImage(path); PictureActionArgs args = new PictureActionArgs() { LocalPictureURL = path }; PictureActionCompleted(this, args); //XamCustomImage.xamCustomImage.SetImage(path); if (image.AsPNG().Save(filepath, false)) { } imagePicker.DismissViewController(true, (Action)null); }; imagePicker.Canceled += (sender, e) => { ((UIImagePickerController)sender).DismissViewController(true, null); }; } catch (Exception ex) { var msg = ex.Message; } }
public override void ViewDidLoad () { base.ViewDidLoad (); Title = "Color Controls Pro"; View.BackgroundColor = UIColor.White; cameraButton = UIButton.FromType (UIButtonType.RoundedRect); cameraButton.Frame = new CGRect(10, 60, 90,40); cameraButton.SetTitle ("Camera", UIControlState.Normal); cameraButton.TouchUpInside += (sender, e) => { TweetStation.Camera.TakePicture (this, (obj) =>{ // https://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerControllerDelegate_Protocol/UIImagePickerControllerDelegate/UIImagePickerControllerDelegate.html#//apple_ref/occ/intfm/UIImagePickerControllerDelegate/imagePickerController:didFinishPickingMediaWithInfo: var photo = obj.ValueForKey(new NSString("UIImagePickerControllerOriginalImage")) as UIImage; var meta = obj.ValueForKey(new NSString("UIImagePickerControllerMediaMetadata")) as NSDictionary; sourceImage = photo; displayImage = sourceImage.Scale(new CGSize(300, 200)); imageView.Image = displayImage; sourceMeta = meta; }); }; View.Add (cameraButton); resetButton = UIButton.FromType (UIButtonType.RoundedRect); resetButton.Frame = new CGRect(110, 60, 90, 40); resetButton.SetTitle ("Reset", UIControlState.Normal); resetButton.TouchUpInside += (sender, e) => { sliderS.Value = 1; sliderB.Value = 0; sliderC.Value = 1; HandleValueChanged (sender, e); }; View.Add (resetButton); saveButton = UIButton.FromType (UIButtonType.RoundedRect); saveButton.Frame = new CGRect(210, 60, 90, 40); saveButton.SetTitle ("Save", UIControlState.Normal); saveButton.TouchUpInside += (sender, e) => { ALAssetsLibrary library = new ALAssetsLibrary(); var img = AdjustImage (sourceImage); if (sourceMeta == null) sourceMeta = new NSDictionary(); // when using 'clouds.jpg' library.WriteImageToSavedPhotosAlbum (img.CGImage, sourceMeta, (assetUrl, error) => { Console.WriteLine ("SAVED TO assetUrl:"+assetUrl); new UIAlertView("Saved", "Photo saved to Camera Roll", null, "OK", null).Show (); }); }; View.Add (saveButton); labelC = new UILabel(new CGRect(10, 120, 90, 20)); labelS = new UILabel(new CGRect(10, 160, 90, 20)); labelB = new UILabel(new CGRect(10, 200, 90, 20)); labelC.Text = "Contrast"; labelS.Text = "Saturation"; labelB.Text = "Brightness"; View.Add (labelC); View.Add (labelS); View.Add (labelB); sliderB = new UISlider(new CGRect(100, 120, 210, 20)); sliderS = new UISlider(new CGRect(100, 160, 210, 20)); sliderC = new UISlider(new CGRect(100, 200, 210, 20)); // http://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CoreImageFilterReference/Reference/reference.html#//apple_ref/doc/filter/ci/CIColorControls sliderS.MinValue = 0; sliderS.MaxValue = 2; sliderS.Value = 1; sliderB.MinValue = -1; sliderB.MaxValue = 1; sliderB.Value = 0; sliderC.MinValue = 0; sliderC.MaxValue = 4; sliderC.Value = 1; sliderC.TouchUpInside += HandleValueChanged; sliderS.TouchUpInside += HandleValueChanged; sliderB.TouchUpInside += HandleValueChanged; View.Add (sliderC); View.Add (sliderS); View.Add (sliderB); imageView = new UIImageView(new CGRect(10, 240, 300, 200)); sourceImage = UIImage.FromFile ("clouds.jpg"); displayImage = sourceImage; imageView.Image = displayImage; View.Add (imageView); if (!UIImagePickerController.IsSourceTypeAvailable (UIImagePickerControllerSourceType.Camera)) { cameraButton.SetTitle ("No camera", UIControlState.Disabled); cameraButton.SetTitleColor (UIColor.Gray, UIControlState.Disabled); cameraButton.Enabled = false; } }
public bool SavePhotoAsync(byte[] data, string directory, string folder, string filename, ref string fullpath) { try { ////code1 //NSData nsData = NSData.FromArray(data); //UIImage image = new UIImage(nsData); //TaskCompletionSource<bool> taskCompletionSource = new TaskCompletionSource<bool>(); //image.SaveToPhotosAlbum((UIImage img, NSError error) => //{ // taskCompletionSource.SetResult(error == null); //}); //return taskCompletionSource.Task; //Code2 var AbsolutePath = string.Empty; // First, check to see if we have initially asked the user for permission // to access their photo album. if (Photos.PHPhotoLibrary.AuthorizationStatus == Photos.PHAuthorizationStatus.NotDetermined) { var status = Plugin.Permissions.CrossPermissions.Current.RequestPermissionsAsync( Plugin.Permissions.Abstractions.Permission.Photos); } if (Photos.PHPhotoLibrary.AuthorizationStatus == Photos.PHAuthorizationStatus.Authorized) { NSData nsData = NSData.FromArray(data); UIImage image = new UIImage(nsData); TaskCompletionSource <bool> taskCompletionSource = new TaskCompletionSource <bool>(); var dict = new NSDictionary(); // This bit of code saves to the Photo Album with metadata ALAssetsLibrary library = new ALAssetsLibrary(); library.WriteImageToSavedPhotosAlbum(image.CGImage, dict, (assetUrl, error) => { AbsolutePath = assetUrl.ToString(); library.AssetForUrl(assetUrl, delegate(ALAsset asset) { AbsolutePath = assetUrl.LastPathComponent; ALAssetRepresentation representation = asset.DefaultRepresentation; if (representation != null) { string fileName = representation.Filename != null ? representation.Filename : string.Empty; 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]; taskCompletionSource.SetResult(error == null); } }, delegate(NSError err) { Console.WriteLine("User denied access to photo Library... {0}", err); }); }); fullpath = AbsolutePath; return(true); } else { return(false); } //Code3 //bool success = false; //NSData imgData = NSData.FromArray(data); //UIKit.UIImage photo = new UIImage(imgData); //var savedImageFilename = System.IO.Path.Combine(directory, "temp"); //savedImageFilename = System.IO.Path.Combine(savedImageFilename, filename); //NSFileManager.DefaultManager.CreateDirectory(savedImageFilename, true, null); //if (System.IO.Path.GetExtension(filename).ToLower() == ".png") // imgData = photo.AsPNG(); //else // imgData = photo.AsJPEG(100); ////File.WriteAllBytes(savedImageFilename, data); //NSError err = null; //success = imgData.Save(savedImageFilename, NSDataWritingOptions.Atomic, out err); //return Task.FromResult(success); } catch (Exception ex) { return(false); } }