Example #1
0
		public static UIImage CopyAndDispose(UIImage original)
		{
			UIGraphics.BeginImageContextWithOptions(original.Size, false, 0);
			original.Draw(new RectangleF(0, 0, original.Size.Width, original.Size.Height));
			UIImage copy = UIGraphics.GetImageFromCurrentImageContext();
		  	UIGraphics.EndImageContext();	
		    original.Dispose();
			return copy;
		}
Example #2
0
 public static void ImageToByteArray(UIImage image, out byte[] mediaByteArray)
 {
     using (NSData imgData = image.AsJPEG ())
     {
         mediaByteArray = new byte[imgData.Length];
         System.Runtime.InteropServices.Marshal.Copy (imgData.Bytes, mediaByteArray, 0, Convert.ToInt32 (imgData.Length));
     }
     image.Dispose ();
 }
        void OnPhotoChosen(UIImagePickerController picker, UIImage photo)
        {
            Photograph = PhotoManager.ScaleToSize (photo, (int) PhotoWidth, (int) PhotoHeight);

            if (picker.SourceType == UIImagePickerControllerSourceType.Camera)
                photo.SaveToPhotosAlbum (OnPhotoSaved);
            else
                photo.Dispose ();

            popover.Dismiss (true);
        }
 static void OnPhotoSaved(UIImage photo, NSError error)
 {
     // dispose of the full-size photograph
     photo.Dispose ();
 }
        private static UIImage ScaleImage(UIImage image, int maxSize)
        {
            UIImage res = image;

            CGImage imageRef = image.CGImage;

            CGImageAlphaInfo alphaInfo = imageRef.AlphaInfo;
            CGColorSpace colorSpaceInfo = CGColorSpace.CreateDeviceRGB();
            if (alphaInfo == CGImageAlphaInfo.None)
            {
                alphaInfo = CGImageAlphaInfo.NoneSkipLast;
            }

            int width = imageRef.Width;
            int height = imageRef.Height;

            if (maxSize > 0 && maxSize < Math.Max(width, height))
            {
                try
                {
                    if (height >= width)
                    {
                        width = (int) Math.Floor(width*(maxSize/(double) height));
                        height = maxSize;
                    }
                    else
                    {
                        height = (int) Math.Floor(height*(maxSize/(double) width));
                        width = maxSize;
                    }

                    int bytesPerRow = (int) image.Size.Width*4;
                    var buffer = new byte[(int) (bytesPerRow*image.Size.Height)];

                    CGBitmapContext bitmap;
                    if (image.Orientation == UIImageOrientation.Up || image.Orientation == UIImageOrientation.Down)
                        bitmap = new CGBitmapContext(buffer, width, height, imageRef.BitsPerComponent,
                            imageRef.BytesPerRow, colorSpaceInfo, alphaInfo);
                    else
                        bitmap = new CGBitmapContext(buffer, height, width, imageRef.BitsPerComponent,
                            imageRef.BytesPerRow, colorSpaceInfo, alphaInfo);

                    switch (image.Orientation)
                    {
                        case UIImageOrientation.Left:
                            bitmap.RotateCTM((float) Math.PI/2);
                            bitmap.TranslateCTM(0, -height);
                            break;
                        case UIImageOrientation.Right:
                            bitmap.RotateCTM(-((float) Math.PI/2));
                            bitmap.TranslateCTM(-width, 0);
                            break;
                        case UIImageOrientation.Up:
                            break;
                        case UIImageOrientation.Down:
                            bitmap.TranslateCTM(width, height);
                            bitmap.RotateCTM(-(float) Math.PI);
                            break;
                    }

                    bitmap.DrawImage(new RectangleF(0, 0, width, height), imageRef);
                    res = UIImage.FromImage(bitmap.ToImage());
                }
                finally
                {
                    image.Dispose();
                }
            }


            return res;
        }
		void MediaChooser (object sender, UIButtonEventArgs e)
		{
			if (e.ButtonIndex == 1)
			{
			
				System.Console.WriteLine ("Camera");

				var picker = new MediaPicker ();
				//           new MediaPicker (this); on Android
				if (!picker.IsCameraAvailable)
				{
					Console.WriteLine("No camera!");				
				}
				else 
				{
					picker.TakePhotoAsync 
						(
						  new StoreCameraMediaOptions 
							{
							  Name = "test.jpg"
							, Directory = "MediaPickerSample"
							}
						).ContinueWith 
							(
								t =>
								{
									if (t.IsCanceled) 
									{
									Console.WriteLine ("User canceled");
									return;
								}
								Console.WriteLine (t.Result.Path);
								imageView.Image = new UIImage(t.Result.Path);
								}
							, TaskScheduler.FromCurrentSynchronizationContext()
							);
				}

				image_bytes  = UIImageViewToByteArray(imageView.Image);

				image.Dispose ();

			} 
			else if (e.ButtonIndex == 2) 
			{
			
				System.Console.WriteLine ("Library");

				var picker = new MediaPicker ();
				//           new MediaPicker (this); on Android
			
				picker.PickPhotoAsync()
					.ContinueWith (t => {
						if (t.IsCanceled) {
							Console.WriteLine ("User canceled");
							return;
						}
						Console.WriteLine (t.Result.Path);
						imageView.Image = new UIImage(t.Result.Path);
					}, TaskScheduler.FromCurrentSynchronizationContext());
				}

			//MOKEEEEEEEEE UIImage To ByteArray
			image = imageView.Image;

			image_bytes = UIImageViewToByteArray(image);

			image.Dispose ();
		}
Example #7
0
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            window = new UIWindow (UIScreen.MainScreen.Bounds);
            Otazky = db.Get();
            SetRandom();

            SetRootSections();

            var o = new Olenenok(root);

            viewController = o;

            window.RootViewController = viewController;

            viewController.OrientationChanged += (object sender, EventArgs e) => {
                if(Current.Url != "placeholder") {

                float _scale =
                    (UIDevice.CurrentDevice.Orientation.ToString() == UIInterfaceOrientation.LandscapeLeft.ToString() ||
                     UIDevice.CurrentDevice.Orientation.ToString() == UIInterfaceOrientation.LandscapeRight.ToString()) ? 1.04f : 1.6f;

                var _i = UIImage.FromBundle("Images/"+ Current.Url);
                var _i2 = new UIImage(_i.CGImage, _scale, UIImageOrientation.Up);

                var _img = new UIImageView(_i2);

                picSection.Clear();
                picSection.Add(_img);

                _i.Dispose(); _i2.Dispose();

                //picSection.Reload(picSection, UITableViewRowAnimation.Fade);
                root.Reload(picSection, UITableViewRowAnimation.Fade);
                }

            };

            GestureFuch();

            window.MakeKeyAndVisible ();

            return true;
        }
Example #8
0
        static void SetRootSections()
        {
            if(root == null) {
                root = new RootElement(null);
                sections = new List<Section>();
            }

            else {
                sections.ForEach(s =>
                                 root.Remove(s, UITableViewRowAnimation.Fade));
                sections = new List<Section>();

            }

            var queSection = new Section() { MakeElement(Current.Question, true) };

            var answers = new Section();

            picSection = new Section();
            if(Current.Url != "placeholder") {

                float scale =
                    (UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.LandscapeLeft ||
                     UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.LandscapeRight) ? 1.04f : 1.6f;

                var i = UIImage.FromBundle("Images/"+ Current.Url);
                var i2 = new UIImage(i.CGImage, scale, UIImageOrientation.Up);

                var img = new UIImageView(i2);

                i.Dispose();
                i2.Dispose();

                picSection.Add(img);

                root.Add(picSection);
                sections.Add(picSection);

            }

            root.Add(queSection);
            sections.Add(queSection);

            var a1 = MakeElement(Current.Answer1);
            var a2 = MakeElement(Current.Answer2);
            var a3 = MakeElement(Current.Answer3);

            answers.Add(a1);
            answers.Add(a2);

            if(Current.Answer3 != null) {
                answers.Add(a3);
            }

            root.Add(answers);
            sections.Add(answers);

            NSAction action = () => {
                if(Current.Right == 1) {
                    a1.Accessory = UITableViewCellAccessory.Checkmark;
                    a1.GetImmediateRootElement().Reload(answers, UITableViewRowAnimation.Fade);
                }
                if(Current.Right == 2) {
                    a2.Accessory = UITableViewCellAccessory.Checkmark;
                    a2.GetImmediateRootElement().Reload(answers, UITableViewRowAnimation.Fade);
                }
                if(Current.Right == 3) {
                    a3.Accessory = UITableViewCellAccessory.Checkmark;
                    a3.GetImmediateRootElement().Reload(answers, UITableViewRowAnimation.Fade);
                }
            };

            a1.Tapped += action;
            a2.Tapped += action;
            a3.Tapped += action;
        }