Exemplo n.º 1
0
        public AnimatedImage(CGImageSource source)
        {
            imageSource = source;
            FrameCount  = imageSource.ImageCount;

            var imageProperties = source.CopyProperties(new CGImageOptions());

            if (imageProperties != null)
            {
                LoopCount = LoopCountForProperties(imageProperties);
            }
            else
            {
                // The default loop count for a GIF with no loop count specified is 1.
                // Infinite loops are indicated by an explicit value of 0 for this property.
                LoopCount = 1;
            }

            var firstImage = source.CreateImage(0, null);

            if (firstImage != null)
            {
                Size = new CGSize(firstImage.Width, firstImage.Height);
            }
            else
            {
                Size = CGSize.Empty;
            }

            var delayTimes    = Enumerable.Repeat(1.0 / 30.0, (int)FrameCount).ToArray();
            var totalDuration = 0.0;

            for (var index = 0; index < FrameCount; index++)
            {
                var properties = source.CopyProperties(new CGImageOptions(), index);
                if (properties != null)
                {
                    var time = FrameDelayForProperties(properties);
                    if (time != null)
                    {
                        delayTimes[index] = time.Value;
                    }
                }
                totalDuration += delayTimes[index];
            }
            Duration = totalDuration;
            delays   = delayTimes;
        }
Exemplo n.º 2
0
        public Size GetImageSize(string imagePath)
        {
            try
            {
                NSUrl         url           = new NSUrl(path: imagePath, isDir: false);
                CGImageSource myImageSource = CGImageSource.FromUrl(url, null);
                var           ns            = new NSDictionary();

                //Dimensions
                NSObject width;
                NSObject height;

                using (NSDictionary imageProperties = myImageSource.CopyProperties(ns, 0))
                {
                    var tiff = imageProperties.ObjectForKey(CGImageProperties.TIFFDictionary) as NSDictionary;
                    width  = imageProperties[CGImageProperties.PixelWidth];
                    height = imageProperties[CGImageProperties.PixelHeight];
                }

                return(new Size(Convert.ToInt32(height.ToString()),
                                Convert.ToInt32(width.ToString())));
            }
            catch
            {
                return(Size.Empty);
            }
        }
Exemplo n.º 3
0
        //http://chadkuehn.com/read-metadata-from-photos-in-c/
        private DateTime?ExtractDateTimeTaken(string path)
        {
            DateTime?     dt              = null;
            NSUrl         url             = new NSUrl(path: path, isDir: false);
            CGImageSource myImageSource   = CGImageSource.FromUrl(url, null);
            var           ns              = new NSDictionary();
            var           imageProperties = myImageSource.CopyProperties(ns, 0);

            var tiff = imageProperties.ObjectForKey(CGImageProperties.TIFFDictionary) as NSDictionary;
            var exif = imageProperties.ObjectForKey(CGImageProperties.ExifDictionary) as NSDictionary;

            //DateTaken
            var dtstr = (exif[CGImageProperties.ExifDateTimeOriginal]).ToString();

            if (string.IsNullOrEmpty(dtstr))
            {
                dtstr = (tiff[CGImageProperties.TIFFDateTime]).ToString();
            }
            if (!string.IsNullOrEmpty(dtstr))
            {
                dt = DateTime.ParseExact(dtstr, "yyyy:MM:dd HH:mm:ss", CultureInfo.InvariantCulture);
            }

            return(dt);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Extract a DateTime object from a NSUrl object
        /// </summary>
        /// <param name="url">The url of the file</param>
        /// <returns>
        /// 1. The current date as a Nullable<DateTime> object IF url == null
        /// 2. The date (as a Nullable<DateTime> object) that exists within the file's metadata (pointed to by url) IFF the EXIF data exists
        /// 3. Null if the date cannot be found within the file's metadata
        /// </returns>
        private DateTime?GetDate(NSUrl url)
        {
            DateTime dateTaken;

            // If the provided url is null
            // NOTE: This case will happen if we import from camera
            if (url == null)
            {
                dateTaken = DateTime.Now;
                return(dateTaken);
            }

            CGImageSource source     = CGImageSource.FromUrl(url);
            NSDictionary  ns         = new NSDictionary();
            NSDictionary  properties = source.CopyProperties(ns, 0);

            NSDictionary exifDict = properties?.ObjectForKey(ImageIO.CGImageProperties.ExifDictionary) as NSDictionary;

            if (exifDict != null)
            {
                NSString date = exifDict[ImageIO.CGImageProperties.ExifDateTimeOriginal] as NSString;

                if (!string.IsNullOrEmpty(date) &&
                    DateTime.TryParseExact(date, "yyyy:MM:dd HH:mm:ss", CultureInfo.CurrentCulture, DateTimeStyles.None, out dateTaken))
                {
                    return(dateTaken);
                }
            }

            return(null);
        }
Exemplo n.º 5
0
        public object GetImageFromUri(FileUri imageUri, int targetWidth, int targetHeight, int rotation)
        {
            using (Stream stream = GetFileSystem().OpenFile(imageUri, UniversalFileMode.Open, UniversalFileAccess.Read, UniversalFileShare.Read))
            {
                CGImageSource source = CGImageSource.FromUrl(imageUri.ToNSUrl());

                CGImageOptions options = new CGImageOptions();
                options.ShouldCache = false;

                var props = source.CopyProperties(options, 0);

                int imageWidth  = ((NSNumber)props["PixelWidth"]).Int32Value;
                int imageHeight = ((NSNumber)props["PixelHeight"]).Int32Value;

                int scale = 1;
                while (imageWidth / scale / 2 >= (nfloat)targetWidth &&
                       imageHeight / scale / 2 >= (nfloat)targetHeight)
                {
                    scale *= 2;
                }

                stream.Seek(0, SeekOrigin.Begin);
                UIImage image = UIImage.LoadFromData(NSData.FromUrl(imageUri.ToNSUrl()), scale);

                if (rotation != 0)
                {
                    float radians = rotation * (float)Math.PI / 180f;

                    CGRect            imageFrame = new CGRect(0, 0, image.Size.Width, image.Size.Height);
                    CGAffineTransform t          = CGAffineTransform.MakeRotation(radians);
                    imageFrame = t.TransformRect(imageFrame);

                    CGSize rotatedSize = new CGSize(imageFrame.Width, imageFrame.Height);

                    UIGraphics.BeginImageContext(rotatedSize);

                    using (CGContext context = UIGraphics.GetCurrentContext())
                    {
                        context.TranslateCTM(rotatedSize.Width / 2, rotatedSize.Height / 2);
                        context.RotateCTM(-radians);
                        context.ScaleCTM(1.0f, -1.0f);
                        image.Draw(new CGRect(-image.Size.Width / 2, -image.Size.Height / 2, image.Size.Width, image.Size.Height));

                        image = UIGraphics.GetImageFromCurrentImageContext();
                    }

                    UIGraphics.EndImageContext();
                }

                return(image);
            }
        }
Exemplo n.º 6
0
        public static UIImage LoadGIFImage(string gifImage)
        {
            try
            {
                using (NSData imgData = NSData.FromFile(gifImage))
                    using (CGImageSource imgSource = CGImageSource.FromData(imgData))
                    {
                        NSString      gifKey      = new NSString(GIFKey);
                        NSString      gifDelayKey = new NSString(GIFDelayTimeKey);
                        List <double> frameDelays = new List <double>();

                        // Array that will hold each GIF frame.
                        UIImage[] frames = new UIImage[imgSource.ImageCount];

                        // Get all individual frames and their delay time.
                        for (int i = 0; i < imgSource.ImageCount; i++)
                        {
                            // Get the frame's property dictionary and the '{GIF}' dictionary from that.
                            using (NSDictionary frameProps = imgSource.CopyProperties(null, i))
                                using (NSMutableDictionary gifDict = (NSMutableDictionary)frameProps[gifKey])
                                {
                                    double frameDelay = ((NSNumber)gifDict[gifDelayKey]).DoubleValue;
                                    frameDelays.Add(frameDelay);
                                }                //end using

                            // Fill the array.
                            using (CGImage cgImage = imgSource.CreateImage(i, null))
                            {
                                frames[i] = UIImage.FromImage(cgImage);
                            }            //end using cgImage
                        }                //end for

                        // Create animated image.
                        return(UIImage.CreateAnimatedImage(frames, frameDelays.Sum()));
                    }            //end using
            } catch (Exception ex)
            {
                // Something went wrong!
                throw ex;
            }    //end try catch
        }        //end static UIImage LoadGifImage
Exemplo n.º 7
0
        public IExifData GetExifData()
        {
            if (info != null)
            {
                return(new ExifData(info.ObjectForKey(UIImagePickerController.MediaMetadata) as NSDictionary));
            }

            if (filePath != null)
            {
                NSUrl url = System.IO.File.Exists(filePath) ? NSUrl.FromFilename(filePath) : NSUrl.FromString(filePath);
                if (url != null)
                {
                    CGImageSource source = CGImageSource.FromUrl(url);
                    if (source != null)
                    {
                        return(new ExifData(source.CopyProperties((NSDictionary)null, 0)));
                    }
                }
            }

            return(new ExifData());
        }
Exemplo n.º 8
0
 public static Size? Measure (CGImageSource source)
 {
     lock (_gate) {
         if (source == null || source.Handle == IntPtr.Zero)
             return null;
         
         try {
             var props = source.CopyProperties ((NSDictionary) null, 0);
             if (props == null || !props.ContainsKey ((NSString) "PixelHeight") || !props.ContainsKey ((NSString) "PixelWidth"))
                 return null;
             
             var orientation = ReadOrientation (props);
             
             return ApplyOrientation (new Size (
                 ((NSNumber) props ["PixelWidth"]).IntValue,
                 ((NSNumber) props ["PixelHeight"]).IntValue
             ), orientation);
             
         } catch {
             return null;
         }
     }
 }