Exemple #1
0
//		static CCVerticalTextAlignment vertical;
//		static CCTextAlignment horizontal;

        // Used for debuggin purposes
        internal static void SaveToFile(string fileName, CGBitmapContext bitmap)
        {
            if (bitmap == null)
            {
                throw new ObjectDisposedException("cgimage");
            }

            // With MonoTouch we can use UTType from CoreMobileServices but since
            // MonoMac does not have that yet (or at least can not find it) I will
            // use the string version of those for now.  I did not want to add another
            // #if #else in here.

            // for now we will just default this to png
            var typeIdentifier = "public.png";

            // * NOTE * we only support one image for right now.
            //NSMutableData imgData = new NSMutableData();
            NSUrl url = NSUrl.FromFilename(fileName);

            // Create an image destination that saves into the imgData
            #if IOS
            CGImageDestination dest = CGImageDestination.Create(url, typeIdentifier, 1);
            #else
            CGImageDestination dest = CGImageDestination.FromUrl(url, typeIdentifier, 1);
            #endif

            // Add an image to the destination
            dest.AddImage(bitmap.GetImage(), (NSDictionary)null);

            // Finish the export
            //bool success = dest.Close ();
            //                        if (success == false)
            //                                Console.WriteLine("did not work");
            //                        else
            //                                Console.WriteLine("did work: " + path);

            dest.Dispose();
            dest = null;
        }
            void CameraImageCaptured(object sender, UIImagePickerMediaPickedEventArgs e)
            {
                bool   result    = false;
                string imagePath = null;

                // create a url of the path for the file to write
                NSUrl imageDestUrl = NSUrl.CreateFileUrl(new string[] { ImageDest });

                // create a CGImage destination that converts the image to jpeg
                CGImageDestination cgImageDest = CGImageDestination.Create(imageDestUrl, MobileCoreServices.UTType.JPEG, 1);

                if (cgImageDest != null)
                {
                    // note: the edited image is saved "correctly", so we don't have to rotate.

                    // rotate the image 0 degrees since we consider portrait to be the default position.
                    CIImage ciImage = new CIImage(e.OriginalImage.CGImage);

                    float rotationDegrees = 0.00f;
                    switch (e.OriginalImage.Orientation)
                    {
                    case UIImageOrientation.Up:
                    {
                        // don't do anything. The image space and the user space are 1:1
                        break;
                    }

                    case UIImageOrientation.Left:
                    {
                        // the image space is rotated 90 degrees from user space,
                        // so do a CCW 90 degree rotation
                        rotationDegrees = 90.0f;
                        break;
                    }

                    case UIImageOrientation.Right:
                    {
                        // the image space is rotated -90 degrees from user space,
                        // so do a CW 90 degree rotation
                        rotationDegrees = -90.0f;
                        break;
                    }

                    case UIImageOrientation.Down:
                    {
                        rotationDegrees = 180;
                        break;
                    }
                    }

                    // create our transform and apply it to the image
                    CGAffineTransform transform = CGAffineTransform.MakeIdentity( );
                    transform.Rotate(rotationDegrees * Rock.Mobile.Math.Util.DegToRad);
                    CIImage rotatedImage = ciImage.ImageByApplyingTransform(transform);

                    // create a context and render it back out to a CGImage. (Cast to ints so we account for any floating point error)
                    CIContext ciContext      = CIContext.FromOptions(null);
                    CGImage   rotatedCGImage = ciContext.CreateCGImage(rotatedImage, new System.Drawing.RectangleF((int)rotatedImage.Extent.X,
                                                                                                                   (int)rotatedImage.Extent.Y,
                                                                                                                   (int)rotatedImage.Extent.Width,
                                                                                                                   (int)rotatedImage.Extent.Height));

                    // put the image in the destination, converting it to jpeg.
                    cgImageDest.AddImage(rotatedCGImage);


                    // close and dispose.
                    if (cgImageDest.Close( ))
                    {
                        result    = true;
                        imagePath = ImageDest;

                        cgImageDest.Dispose( );
                    }
                }

                CameraFinishedCallback(result, imagePath);
            }
        public void Save(string path, ImageFormat format)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (NativeCGImage == null)
            {
                throw new ObjectDisposedException("cgimage");
            }

            // With MonoTouch we can use UTType from CoreMobileServices but since
            // MonoMac does not have that yet (or at least can not find it) I will
            // use the string version of those for now.  I did not want to add another
            // #if #else in here.


            // for now we will just default this to png
            var typeIdentifier = "public.png";

            // Get the correct type identifier
            if (format == ImageFormat.Bmp)
            {
                typeIdentifier = "com.microsoft.bmp";
            }
//			else if (format == ImageFormat.Emf)
//				typeIdentifier = "image/emf";
//			else if (format == ImageFormat.Exif)
//				typeIdentifier = "image/exif";
            else if (format == ImageFormat.Gif)
            {
                typeIdentifier = "com.compuserve.gif";
            }
            else if (format == ImageFormat.Icon)
            {
                typeIdentifier = "com.microsoft.ico";
            }
            else if (format == ImageFormat.Jpeg)
            {
                typeIdentifier = "public.jpeg";
            }
            else if (format == ImageFormat.Png)
            {
                typeIdentifier = "public.png";
            }
            else if (format == ImageFormat.Tiff)
            {
                typeIdentifier = "public.tiff";
            }
            else if (format == ImageFormat.Wmf)
            {
                typeIdentifier = "com.adobe.pdf";
            }

            // Not sure what this is yet
            else if (format == ImageFormat.MemoryBmp)
            {
                throw new NotImplementedException("ImageFormat.MemoryBmp not supported");
            }

            // Obtain a URL file path to be passed
            NSUrl url = NSUrl.FromFilename(path);

            // * NOTE * we only support one image for right now.

            // Create an image destination that saves into the path that is passed in
            CGImageDestination dest = CGImageDestination.FromUrl(url, typeIdentifier, imageCount, null);

            // Add an image to the destination
            dest.AddImage(NativeCGImage, null);

            // Finish the export
            bool success = dest.Close();

//                        if (success == false)
//                                Console.WriteLine("did not work");
//                        else
//                                Console.WriteLine("did work: " + path);
            dest.Dispose();
            dest = null;
        }