Beispiel #1
0
 public static void RemoveImageFromCache(string imagePath)
 {
     SDWebImageManager.SharedManager.ImageCache.RemoveImage(EnvironmentConstants.getS3Url() + imagePath, true, completion: () =>
     {
         Console.WriteLine(EnvironmentConstants.getS3Url() + imagePath + " removed from cache");
     });
 }
Beispiel #2
0
        public static UIImage GetImageFromNSUrl(string imagePath)
        {
            UIImage result = null;

            SDWebImageManager.SharedManager.Download(new NSUrl(EnvironmentConstants.getS3Url() + imagePath), SDWebImageOptions.ProgressiveDownload, //HighPriority
                                                     progressBlock: (receivedSize, completedSize) =>
            {
                //do nothing
            },
                                                     completedBlock: (image, error, cacheType, finished, imageUrl) =>
            {
                Console.WriteLine("Image = " + image);
                Console.WriteLine("Error = " + error);
                Console.WriteLine("Cache Type = " + cacheType);
                Console.WriteLine("Finished = " + finished);

                if (error != null && error.ToString().Contains("1100"))
                {
                    SDWebImageManager.SharedManager.Download(new NSUrl(EnvironmentConstants.getS3Url() + imagePath), SDWebImageOptions.RetryFailed,
                                                             progressBlock: (receivedSize, completedSize) =>
                    {
                        //do nothing
                    },
                                                             completedBlock: (image1, error1, cacheType1, finished1, imageUrl1) =>
                    {
                        Console.WriteLine("Image1 = " + image);
                        Console.WriteLine("Error1 = " + error);
                        Console.WriteLine("Cache Type1 = " + cacheType);
                        Console.WriteLine("Finished1 = " + finished);

                        if (image1 != null)
                        {
                            result = image1;
                        }
                    });
                }

                if (image != null && result == null)
                {
                    result = image;
                }
            }
                                                     );

            return(result);
        }
Beispiel #3
0
        //public static void SetImageFromNSUrlSession(string imagePath, UIImageView imageView, NSCache imageCache = null)
        //{
        //    UIImage imageFromCache = null;
        //    var url = new NSUrl(EnvironmentConstants.getS3Url() + imagePath);
        //    var noCacheStr = "?nocache=" + String.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now);
        //    var fetchUrl = new NSUrl(EnvironmentConstants.getS3Url() + imagePath + noCacheStr);

        //    if (imageCache != null)
        //    {
        //        imageFromCache = (UIImage)imageCache.ObjectForKey(NSString.FromObject(url.AbsoluteString));
        //    }

        //    if (imageFromCache != null)
        //    {
        //        imageView.Image = imageFromCache;
        //    }
        //    else
        //    {
        //        var task = NSUrlSession.SharedSession.CreateDataTask(fetchUrl, (data, response, error) =>
        //        {
        //            try
        //            {
        //                DispatchQueue.MainQueue.DispatchAsync(() =>
        //                {
        //                    if (response != null && ((NSHttpUrlResponse)response).StatusCode != 403 && error == null)
        //                    {
        //                        if (imageCache != null)
        //                        {
        //                            var imageToCache = UIImage.LoadFromData(data);

        //                            imageView.Image = imageToCache;

        //                            if (imageToCache != null)
        //                            {
        //                                imageCache.SetObjectforKey(imageToCache, NSString.FromObject(url.AbsoluteString));
        //                            }
        //                        }
        //                        else
        //                        {
        //                            imageView.Image = UIImage.LoadFromData(data);
        //                        }
        //                    }
        //                });
        //            }
        //            catch (Exception ex)
        //            {
        //                Utils.HandleException(ex);
        //            }
        //        });
        //        task.Resume();
        //    }
        //}

        public static void SetImageFromNSUrlSession(string imagePath, UIImageView imageView, NSObject controller, PictureType picType)
        {
            UIImage placeholder = UIImage.FromBundle("ImagePlaceholder");

            try
            {
                if (picType.Equals(PictureType.Profile))
                {
                    placeholder = UIImage.FromBundle("Profile");
                }

                if (picType.Equals(PictureType.Group))
                {
                    placeholder = UIImage.FromBundle("Group");
                }

                imageView.SetImage(new NSUrl(EnvironmentConstants.getS3Url() + imagePath), placeholder, SDWebImageOptions.ProgressiveDownload, //HighPriority
                                   progressBlock: (receivedSize, completedSize) =>
                {
                    if (activityIndicator == null)
                    {
                        controller.InvokeOnMainThread(() =>
                        {
                            activityIndicator = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.Gray);
                            imageView.AddSubview(activityIndicator);
                            activityIndicator.Center = imageView.Center;
                            activityIndicator.StartAnimating();
                        });
                    }
                },
                                   completedBlock: (image, error, cacheType, finished) =>
                {
                    Console.WriteLine("Image = " + image);
                    Console.WriteLine("Error = " + error);
                    Console.WriteLine("Cache Type = " + cacheType);
                    Console.WriteLine("Finished = " + finished);

                    if (activityIndicator != null)
                    {
                        controller.InvokeOnMainThread(() =>
                        {
                            activityIndicator.RemoveFromSuperview();
                            activityIndicator = null;
                        });
                    }

                    if (error != null && error.ToString().Contains("1100"))
                    {
                        imageView.SetImage(new NSUrl(EnvironmentConstants.getS3Url() + imagePath), placeholder, SDWebImageOptions.RetryFailed);
                    }

                    if (image != null)
                    {
                        imageView.Image = image;
                    }
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }