Monotouch takes photos and uploads images,
Not to mention, the above Code:
Select Image Upload ========
Using System; using System. drawing; using MonoTouch. assetsLibrary; using MonoTouch. UIKit; using MonoTouch. foundation; namespace world {public class ImageViewController: UIViewController {UIImagePickerController imagePicker; UIButton choosePhotoButton; UIImageView imageView; public ImageViewController () {} public override void Merge (bool animated) {base. viewWillAppear (animated); this. navigationContro Roller. setNavigationBarHidden (false, false);} public override void ViewDidLoad () {base. viewDidLoad (); Title = "select image"; View. backgroundColor = UIColor. white; imageView = new UIImageView (new RectangleF (10, 80,300,300); Add (imageView); choosePhotoButton = UIButton. fromType (UIButtonType. roundedRect); choosePhotoButton. frame = new RectangleF (10,200,100, 40); choosePhotoButton. setTitle ("Picker", UICon TrolState. normal); choosePhotoButton. touchUpInside + = (s, e) =>{// create a new picker controllerimagePicker = new UIImagePickerController (); // set our source to the photo libraryimagePicker. sourceType = UIImagePickerControllerSourceType. photoLibrary; // set what media typesimagePicker. mediaTypes = UIImagePickerController. availableMediaTypes (UIImagePickerControllerSourceType. photoLibrary); imagePi Cker. finishedPickingMedia + = Handle_FinishedPickingMedia; imagePicker. canceled + = Handle_Canceled; // show the pickerNavigationController. presentViewController (imagePicker, true, null) ;}; View. add (choosePhotoButton);} // Do something when thevoid Handle_Canceled (object sender, EventArgs e) {Console. writeLine ("picker canceled"); imagePicker. dismissViewController (true, null);} // This is a sample met Hod that handles the FinishedPickingMediaEventprotected void Handle_FinishedPickingMedia (object sender, inclue) {// determine what was selected, video or imagebool isImage = false; switch (e. info [UIImagePickerController. mediaType]. toString () {case "public. image ": Console. writeLine ("Image selected"); isImage = true; break; case "public. video ": Console. writeLine ("Video se Lected "); break;} Console. writeLine ("=> Reference URL: [" + UIImagePickerController. referenceUrl + "]"); // get common info (shared between images and video) NSUrl referenceURL = e. info [new NSString ("UIImagePickerControllerReferenceURL")] as NSUrl; if (referenceURL! = Null) Console. writeLine (referenceURL. toString (); // if it was an image, get the other image infoif (isImage) {// get the original imageUIImage originalImage = e. info [UIImagePickerController. originalImage] as UIImage; if (originalImage! = Null) {// do something with the imageConsole. writeLine ("got the original image"); var documentsDirectory = Environment. getFolderPath (Environment. specialFolder. personal); string jpgFilename = System. IO. path. combine (documentsDirectory, DateTime. now. toString ("yyyyMMddHHmmss") + ". jpg "); NSData imgData = originalImage. asJPEG (); NSError err = null; if (imgData. save (jpgFilename, false, out err) {Cons Ole. writeLine ("saved as" + jpgFilename);} else {Console. writeLine ("NOT saved as" + jpgFilename + "because" + err. localizedDescription);} // imageView. image = originalImage; imageView. image = UIImage. fromFile (jpgFilename);} // get the edited imageUIImage editedImage = e. info [UIImagePickerController. editedImage] as UIImage; if (editedImage! = Null) {// do something with the imageConsole. writeLine ("got the edited image"); imageView. image = editedImage;} //-get the image metadataNSDictionary imageMetadata = e. info [UIImagePickerController. mediaMetadata] as NSDictionary; if (imageMetadata! = Null) {// do something with the metadataConsole. writeLine ("got image metadata") ;}// if it's a videoelse {// get video urlNSUrl mediaURL = e. info [UIImagePickerController. mediaURL] as NSUrl; if (mediaURL! = Null) {// Console. WriteLine (mediaURL. ToString () ;}// dismiss the pickerimagePicker. DismissViewController (true, null );}//...}}Note: during debugging from the simulator, You need to paste the selected image and save it before you can select it. Baidu will know about this.
The core code is as follows:
/// Camera. cs: Support code for taking pictures /// Copyright 2010 Miguel de Icaza // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or conflict // Copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: /// The above copyright notice and this permission notice shall be encoded in // all copies or substantial portions of the Software. /// the software is provided "as is", without warranty of any kind, express or // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES O F merchantability, // fitness for a particle purpose and noninfringement. in no event shall the // authors or copyright holders be liable for any claim, damages or other // LIABILITY, whether in an action of contract, tort or otherwise, arising from, // out of or in connection with the software or the use or other dealings in // the software. using System; using MonoTouch. UIKit; using MonoTouch. foundati On; namespace TweetStation {// A static class that will reuse the UIImagePickerController // as iPhoneOS has a crash if multiple UIImagePickerController are created // http://stackoverflow.com/questions/487173// (Follow the links) // public static class Camera {static UIImagePickerController picker; static Action <NSDictionary> _ callback; static void Init () {if (picker! = Null) return; picker = new UIImagePickerController (); picker. delegate = new CameraDelegate ();} class CameraDelegate: callback {public override void FinishedPickingMedia (UIImagePickerController picker, NSDictionary info) {var cb = _ callback; _ callback = null; picker. dismissViewController (true, null); cb (info) ;}// photograph public static void TakePicture (UIViewController parent, Action <NSDictionary> callback) {Init (); picker. sourceType = UIImagePickerControllerSourceType. camera; _ callback = callback; parent. presentViewController (picker, true, null);} public static void SelectPicture (UIViewController parent, Action <NSDictionary> callback) {Init (); picker. sourceType = UIImagePickerControllerSourceType. photoLibrary; _ callback = callback; parent. presentViewController (picker, true, null );}}}
using System;using System.Drawing;using MonoTouch.AssetsLibrary;using MonoTouch.UIKit;using MonoTouch.Foundation;namespace ImageView {public class ImageViewController : UIViewController {UIButton cameraButton;public override void ViewDidLoad (){base.ViewDidLoad ();Title = "Save to Album";View.BackgroundColor = UIColor.White;cameraButton = UIButton.FromType (UIButtonType.RoundedRect);cameraButton.Frame = new RectangleF(10, 200, 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 metadataALAssetsLibrary 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;}}}}By the way,
originalImage.AsJPEG ();
There is
AsStreamProperties, so that you can remotely upload the selected image to the server, you know.