Example #1
0
 public CGImage ImageAtIndex(int index)
 {
     if (index < FrameCount)
     {
         return(imageSource.CreateImage(index, (CGImageOptions)null));
     }
     else
     {
         return(null);
     }
 }
Example #2
0
        private static List <UIImage> GetFrames(CGImageSource source)
        {
            var retval = new List <UIImage>();

            for (int i = 0; i < source?.ImageCount; i++)
            {
                using (var frameImage = source.CreateImage(i, null))
                    retval.Add(UIImage.FromImage(frameImage));
            }

            return(retval);
        }
Example #3
0
        private static List <CoreGraphics.CGImage> GetFrames(CGImageSource source)
        {
            var retval = new List <CoreGraphics.CGImage>();

            for (int i = 0; i < source?.ImageCount; i++)
            {
                var frameImage = source.CreateImage(i, null);
                retval.Add(frameImage);
            }

            return(retval);
        }
Example #4
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;
        }
Example #5
0
            public void AddFrameData(int index, CGImageSource imageSource)
            {
                if (index < 0 || index >= _imageCount || index >= imageSource.ImageCount)
                {
                    throw new ArgumentException();
                }

                double delayTime = 0.1f;

                var imageProperties = imageSource.GetProperties(index, null);

                using (var gifImageProperties = imageProperties?.Dictionary[ImageIO.CGImageProperties.GIFDictionary])
                    using (var unclampedDelayTimeValue = gifImageProperties?.ValueForKey(ImageIO.CGImageProperties.GIFUnclampedDelayTime))
                        using (var delayTimeValue = gifImageProperties?.ValueForKey(ImageIO.CGImageProperties.GIFDelayTime))
                        {
                            if (unclampedDelayTimeValue != null)
                            {
                                double.TryParse(unclampedDelayTimeValue.ToString(), out delayTime);
                            }
                            else if (delayTimeValue != null)
                            {
                                double.TryParse(delayTimeValue.ToString(), out delayTime);
                            }

                            // Frame delay compability adjustment.
                            if (delayTime <= 0.02f)
                            {
                                delayTime = 0.1f;
                            }

                            using (var image = imageSource.CreateImage(index, null))
                            {
                                if (image != null)
                                {
                                    Width  = Math.Max(Width, (int)image.Width);
                                    Height = Math.Max(Height, (int)image.Height);

                                    _keyFrames[index]?.Dispose();
                                    _keyFrames[index] = null;

                                    _keyFrames[index]    = NSObject.FromObject(image);
                                    _delayTimes[index]   = delayTime;
                                    _totalAnimationTime += delayTime;
                                }
                            }
                        }
            }
Example #6
0
        private void InitializeImageFrame(int frame)
        {
            if (NativeCGImage != null)
            {
                NativeCGImage.Dispose();
            }

            imageTransform = CGAffineTransform.MakeIdentity();

            SetImageInformation(frame);
            var cg = imageSource.CreateImage(frame, null);

            imageTransform = new CGAffineTransform(1, 0, 0, -1, 0, cg.Height);
            InitWithCGImage(cg);
            GuessPixelFormat();

            currentFrame = frame;
        }
Example #7
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
Example #8
0
        public static UIImage AnimateGif(CGImageSource source, nfloat scale)
        {
            if (source == null)
            {
                return(null);
            }

            var frameCount = source.ImageCount;

            // no need to animate, fail safe.
            if (frameCount <= 1)
            {
                return(UIImage.FromImage(source.CreateImage(0, null), scale, UIImageOrientation.Up));
            }

            var frames         = GetFrames(source);
            var delays         = GetDelays(source);
            var totalDuration  = delays.Sum();
            var adjustedFrames = AdjustFramesToSpoofDurations(frames, scale, delays, totalDuration);

            return(UIImage.CreateAnimatedImage(adjustedFrames.ToArray(), totalDuration / 100.0));
        }
        private static UIImageView CreateAnimatedImageView(CGImageSource imageSource, UIImageView imageView = null)
        {
            var frameCount = imageSource.ImageCount;

            var frameImages    = new List <NSObject>((int)frameCount);
            var frameCGImages  = new List <CGImage>((int)frameCount);
            var frameDurations = new List <double>((int)frameCount);

            var totalFrameDuration = 0.0;

            for (int i = 0; i < frameCount; i++)
            {
                var frameImage = imageSource.CreateImage(i, null);

                frameCGImages.Add(frameImage);
                frameImages.Add(NSObject.FromObject(frameImage));

                var properties = imageSource.GetProperties(i, null);
                var duration   = properties.Dictionary["{GIF}"];
                var delayTime  = duration.ValueForKey(new NSString("DelayTime"));
                duration.Dispose();
                var realDuration = double.Parse(delayTime.ToString());
                frameDurations.Add(realDuration);
                totalFrameDuration += realDuration;
                frameImage.Dispose();
            }

            var      framePercentageDurations       = new List <NSNumber>((int)frameCount);
            var      framePercentageDurationsDouble = new List <double>((int)frameCount);
            NSNumber currentDurationPercentage      = 0.0f;
            double   currentDurationDouble          = 0.0f;

            for (int i = 0; i < frameCount; i++)
            {
                if (i != 0)
                {
                    var previousDuration           = frameDurations[i - 1];
                    var previousDurationPercentage = framePercentageDurationsDouble[i - 1];

                    var number = previousDurationPercentage + (previousDuration / totalFrameDuration);
                    currentDurationDouble     = number;
                    currentDurationPercentage = new NSNumber(number);
                }
                framePercentageDurationsDouble.Add(currentDurationDouble);
                framePercentageDurations.Add(currentDurationPercentage);
            }

            var imageSourceProperties    = imageSource.GetProperties(null);
            var imageSourceGIFProperties = imageSourceProperties.Dictionary["{GIF}"];
            var loopCount            = imageSourceGIFProperties.ValueForKey(new NSString("LoopCount"));
            var imageSourceLoopCount = float.Parse(loopCount.ToString());
            var frameAnimation       = new CAKeyFrameAnimation();

            frameAnimation.KeyPath = "contents";
            if (imageSourceLoopCount <= 0.0f)
            {
                frameAnimation.RepeatCount = float.MaxValue;
            }
            else
            {
                frameAnimation.RepeatCount = imageSourceLoopCount;
            }

            imageSourceGIFProperties.Dispose();


            frameAnimation.CalculationMode     = CAAnimation.AnimationDescrete;
            frameAnimation.Values              = frameImages.ToArray();
            frameAnimation.Duration            = totalFrameDuration;
            frameAnimation.KeyTimes            = framePercentageDurations.ToArray();
            frameAnimation.RemovedOnCompletion = false;
            var firstFrame = frameCGImages[0];

            if (imageView == null)
            {
                imageView = new UIImageView(new CGRect(0.0f, 0.0f, firstFrame.Width, firstFrame.Height));
            }
            else
            {
                imageView.Layer.RemoveAllAnimations();
            }

            imageView.Layer.AddAnimation(frameAnimation, "contents");

            frameAnimation.Dispose();
            return(imageView);
        }
        private static UIImageView CreateAnimatedImageView(CGImageSource imageSource, UIImageView imageView = null)
        {
            var frameCount = imageSource.ImageCount;

            var frameImages = new List<NSObject>((int)frameCount);
            var frameCGImages = new List<CGImage>((int)frameCount);
            var frameDurations = new List<double>((int)frameCount);

            var totalFrameDuration = 0.0;

            for (int i = 0; i < frameCount; i++)
            {
                var frameImage = imageSource.CreateImage(i, null);

                frameCGImages.Add(frameImage);
                frameImages.Add(NSObject.FromObject(frameImage));

                var properties = imageSource.GetProperties(i, null);
                var duration = properties.Dictionary["{GIF}"];
                var delayTime = duration.ValueForKey(new NSString("DelayTime"));
                duration.Dispose ();
                var realDuration = double.Parse(delayTime.ToString());
                frameDurations.Add(realDuration);
                totalFrameDuration += realDuration;
                frameImage.Dispose ();
            }

            var framePercentageDurations = new List<NSNumber>((int)frameCount);
            var framePercentageDurationsDouble = new List<double>((int)frameCount);
            NSNumber currentDurationPercentage = 0.0f;
            double currentDurationDouble = 0.0f;
            for (int i = 0; i < frameCount; i++)
            {
                if (i != 0)
                {
                    var previousDuration = frameDurations[i - 1];
                    var previousDurationPercentage = framePercentageDurationsDouble[i - 1];

                    var number = previousDurationPercentage + (previousDuration/totalFrameDuration);
                    currentDurationDouble = number;
                    currentDurationPercentage = new NSNumber(number);
                }
                framePercentageDurationsDouble.Add(currentDurationDouble);
                framePercentageDurations.Add(currentDurationPercentage);
            }

            var imageSourceProperties = imageSource.GetProperties(null);
            var imageSourceGIFProperties = imageSourceProperties.Dictionary["{GIF}"];
            var loopCount = imageSourceGIFProperties.ValueForKey(new NSString("LoopCount"));
            var imageSourceLoopCount = float.Parse(loopCount.ToString());
            var frameAnimation = new CAKeyFrameAnimation();
            frameAnimation.KeyPath = "contents";
            if (imageSourceLoopCount <= 0.0f)
            {
                frameAnimation.RepeatCount = float.MaxValue;
            }
            else
            {
                frameAnimation.RepeatCount = imageSourceLoopCount;
            }

            imageSourceGIFProperties.Dispose ();


            frameAnimation.CalculationMode = CAAnimation.AnimationDescrete;
            frameAnimation.Values = frameImages.ToArray();
            frameAnimation.Duration = totalFrameDuration;
            frameAnimation.KeyTimes = framePercentageDurations.ToArray();
            frameAnimation.RemovedOnCompletion = false;
            var firstFrame = frameCGImages[0];
            if(imageView == null)
                imageView = new UIImageView(new CGRect(0.0f, 0.0f, firstFrame.Width, firstFrame.Height));
            else
                imageView.Layer.RemoveAllAnimations();

            imageView.Layer.AddAnimation(frameAnimation, "contents");

            frameAnimation.Dispose ();
            return imageView;
        }