Exemplo n.º 1
0
        private static List <int> GetDelays(CGImageSource source)
        {
            var retval = new List <int>();

            for (int i = 0; i < source?.ImageCount; i++)
            {
                var delayCentiseconds = 1;
                var properties        = source.GetProperties(i, null);
                using (var gifProperties = properties.Dictionary[CGImageProperties.GIFDictionary])
                {
                    if (gifProperties != null)
                    {
                        using (var unclampedDelay = gifProperties.ValueForKey(CGImageProperties.GIFUnclampedDelayTime))
                        {
                            double delayAsDouble = unclampedDelay != null?double.Parse(unclampedDelay.ToString(), CultureInfo.InvariantCulture) : 0;

                            if (delayAsDouble == 0)
                            {
                                using (var delay = gifProperties.ValueForKey(CGImageProperties.GIFDelayTime))
                                    delayAsDouble = delay != null?double.Parse(delay.ToString(), CultureInfo.InvariantCulture) : 0;
                            }

                            if (delayAsDouble > 0)
                            {
                                delayCentiseconds = (int)(delayAsDouble * 100);
                            }
                        }
                    }
                }

                retval.Add(delayCentiseconds);
            }

            return(retval);
        }
Exemplo n.º 2
0
        static public FormsCAKeyFrameAnimation CreateAnimationFromCGImageSource(CGImageSource imageSource)
        {
            FormsCAKeyFrameAnimation animation = null;
            float repeatCount = float.MaxValue;
            var   imageCount  = imageSource.ImageCount;

            if (imageCount <= 0)
            {
                return(null);
            }

            using (var imageData = new ImageDataHelper(imageCount))
            {
                if (imageSource.TypeIdentifier == "com.compuserve.gif")
                {
                    var imageProperties = imageSource.GetProperties(null);
                    using (var gifImageProperties = imageProperties?.Dictionary[ImageIO.CGImageProperties.GIFDictionary])
                        using (var repeatCountValue = gifImageProperties?.ValueForKey(ImageIO.CGImageProperties.GIFLoopCount))
                        {
                            if (repeatCountValue != null)
                            {
                                float.TryParse(repeatCountValue.ToString(), out repeatCount);
                            }
                            else
                            {
                                repeatCount = 1;
                            }

                            if (repeatCount == 0)
                            {
                                repeatCount = float.MaxValue;
                            }
                        }
                }

                for (int i = 0; i < imageCount; i++)
                {
                    imageData.AddFrameData(i, imageSource);
                }

                animation = imageData.CreateKeyFrameAnimation();
                if (animation != null)
                {
                    animation.RemovedOnCompletion = false;
                    animation.KeyPath             = "contents";
                    animation.RepeatCount         = repeatCount;
                    animation.Width  = imageData.Width;
                    animation.Height = imageData.Height;

                    if (imageCount == 1)
                    {
                        animation.Duration = double.MaxValue;
                        animation.KeyTimes = null;
                    }
                }
            }

            return(animation);
        }
Exemplo n.º 3
0
        private static int GetLoopCount(CGImageSource source)
        {
            var var = source.GetProperties(null);

            using (var gifProperties = var.Dictionary["{GIF}"])
            {
                var loopCount = gifProperties.ValueForKey(new NSString("LoopCount"));
                return(int.Parse(loopCount.ToString()));
            }
        }
Exemplo n.º 4
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;
                                }
                            }
                        }
            }
Exemplo n.º 5
0
        private static CoreGraphics.CGImageProperties GetImageProperties(CGImageSource imageSource)
        {
            if (imageSource == null)
            {
                return(null);
            }

            try
            {
                // Get original image size
                return(imageSource.GetProperties(0));
            }
            catch (ArgumentNullException ex)
            {
                ImageService.Instance.Config.Logger.Debug(ex.ToString());
                return(null);
            }
        }
Exemplo n.º 6
0
        private static List <double> GetDelays(CGImageSource source)
        {
            var retval = new List <double>();

            for (int i = 0; i < source?.ImageCount; i++)
            {
                var properties = source.GetProperties(i, null);
                using (var gifProperties = properties.Dictionary["{GIF}"])
                {
                    using (var delayTime = gifProperties.ValueForKey(new NSString("DelayTime")))
                    {
                        var realDuration = double.Parse(delayTime.ToString());
                        retval.Add(realDuration);
                    }
                }
            }

            return(retval);
        }
Exemplo n.º 7
0
        private void SetImageInformation(int frame)
        {
            frameCount = (int)imageSource.ImageCount;
            if (frameCount == 0)
            {
                throw new ArgumentException("Invalid image");
            }

            var properties = imageSource.GetProperties(frame, null);

            // This needs to be incorporated in frame information later
            // as well as during the clone methods.
            dpiWidth  = properties.DPIWidthF != null ? (float)properties.DPIWidthF : ConversionHelpers.MS_DPI;
            dpiHeight = properties.DPIWidthF != null ? (float)properties.DPIHeightF : ConversionHelpers.MS_DPI;
            physicalDimension.Width  = (float)properties.PixelWidth;
            physicalDimension.Height = (float)properties.PixelHeight;


            // The physical size may be off on certain implementations.  For instance the dpiWidth and dpiHeight
            // are read using integers in core graphics but in windows it is a float.
            // For example:
            // coregraphics dpiWidth = 24 as integer
            // windows dpiWidth = 24.999935 as float
            // this gives a few pixels difference when calculating the physical size.
            // 256 * 96 / 24 = 1024
            // 256 * 96 / 24.999935 = 983.04
            //
            // https://bugzilla.xamarin.com/show_bug.cgi?id=14365
            // PR: https://github.com/mono/maccore/pull/57
            //

            physicalSize         = new SizeF(physicalDimension.Width, physicalDimension.Height);
            physicalSize.Width  *= ConversionHelpers.MS_DPI / dpiWidth;
            physicalSize.Height *= ConversionHelpers.MS_DPI / dpiHeight;

            // Set the raw image format
            // We will use the UTI from the image source
            rawFormat = ImageFormatFromUTI(imageSource.TypeIdentifier);
        }
        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;
        }