Пример #1
0
        public async Task <UIImage> FetchImage(NSUrl url)
        {
            var dataTaskRequest = await NSUrlSession.SharedSession.CreateDataTaskAsync(url);

            var urlResponse = dataTaskRequest.Response as NSHttpUrlResponse;

            if (urlResponse?.StatusCode != 200 || dataTaskRequest.Data == null)
            {
                // Your application should handle these errors
                // appropriately depending on the kind of error.
                return(null);
            }

            var image = UIImage.LoadFromData(dataTaskRequest.Data);

            if (image != null)
            {
                imageCache.SetObjectforKey(image, new NSString(url.AbsoluteString));
                return(image);
            }
            else
            {
                return(new UIImage());
            }
        }
Пример #2
0
        public static UIImage GetProfileImageforUser(int userid)
        {
            //String usid =Convert.ToString(CurrentUser.RetreiveUserId());
            //NSObject pro = ProfileImages.ObjectForKey(NSObject.FromObject(usid));

            //NSObject profile = profilePics.ObjectForKey(NSObject.FromObject(userid));

            //if (profile != null)
            //	return (UIImage)profile;

            NSData  imgData = null;
            UIImage img     = null;

            try
            {
                url = "https://icsintegration.blob.core.windows.net/profileimages/" + userid + ".jpg";
                NSUrl imageURL = new NSUrl(url);
                imgData = NSData.FromUrl(imageURL);
                img     = UIImage.LoadFromData(imgData);
                profilePics.SetObjectforKey(img, NSObject.FromObject(userid));
            }
            catch (Exception e)
            {
                //LoggingClass.LogError(e.ToString(), screen, e.StackTrace);

                return(null);
            }

            return(img);
        }
        public DataViewController GetViewControllerAt(int index)
        {
            var len = regionCodes.Length;

            if (len == 0 || index >= len)
            {
                return(null);
            }

            var regionCode = regionCodes [index];

            // Return the data view controller for the given index.
            var dataViewController = (DataViewController)cachedDataViewControllers.ObjectForKey((NSString)regionCode);

            if (dataViewController != null)
            {
                return(dataViewController);
            }

            // Create a new view controller and pass suitable data.
            dataViewController            = (DataViewController)storyboard.InstantiateViewController("DataViewController");
            dataViewController.RegionCode = regionCodes [index];

            // Cache the view controller before returning it.
            cachedDataViewControllers.SetObjectforKey(dataViewController, (NSString)regionCode);
            return(dataViewController);
        }
		UIImage DrawHamburger()
		{
			const string hamburgerKey = "Hamburger";
			UIImage img = (UIImage)_nSCache.ObjectForKey((NSString)hamburgerKey);

			if (img != null)
				return img;

			var rect = new CGRect(0, 0, 23f, 23f);

			UIGraphics.BeginImageContextWithOptions(rect.Size, false, 0);
			var ctx = UIGraphics.GetCurrentContext();
			ctx.SaveState();
			ctx.SetStrokeColor(UIColor.Blue.CGColor);

			float size = 3f;
			float start = 4f;
			ctx.SetLineWidth(size);

			for (int i = 0; i < 3; i++)
			{
				ctx.MoveTo(1f, start + i * (size * 2));
				ctx.AddLineToPoint(22f, start + i * (size * 2));
				ctx.StrokePath();
			}

			ctx.RestoreState();
			img = UIGraphics.GetImageFromCurrentImageContext();
			UIGraphics.EndImageContext();

			_nSCache.SetObjectforKey(img, (NSString)hamburgerKey);
			return img;
		}
        private byte[] Put(string cacheName, UIImage image)
        {
            var data = image.AsPNG();

            if (!String.IsNullOrEmpty(cacheName))
            {
                _cache.SetObjectforKey(data, new NSString(cacheName));
            }

            return(data.ToArray());
        }
Пример #6
0
        public void ComposeCell(DataItemCollectionViewCell cell, DataItem dataItem)
        {
            var operationQueue = OperationQueueForCell(cell);

            operationQueue.CancelAllOperations();

            cell.RepresentedDataItem = dataItem;
            cell.Label.Text          = dataItem.Title;
            cell.ImageView.Alpha     = 1f;
            cell.ImageView.Image     = (UIImage)processedImageCache.ObjectForKey((NSString)dataItem.Identifier);

            if (cell.ImageView.Image != null)
            {
                return;
            }

            var processImageOperation = new NSBlockOperation();

            processImageOperation.AddExecutionBlock(() => {
                if (processImageOperation.IsCancelled)
                {
                    return;
                }

                UIImage image = null;
                DispatchQueue.MainQueue.DispatchSync(() => {
                    image = ProcessImageNamed(dataItem.ImageName);
                });

                if (image == null)
                {
                    return;
                }

                processedImageCache.SetObjectforKey(image, (NSString)dataItem.Identifier);
                NSOperationQueue.MainQueue.AddOperation(() => {
                    if (cell.RepresentedDataItem == null)
                    {
                        return;
                    }

                    cell.ImageView.Alpha = 0f;
                    cell.ImageView.Image = image;
                    UIView.Animate(0.25, () => cell.ImageView.Alpha = 1f);
                });
            });

            operationQueue.AddOperation(processImageOperation);
        }
        private UIImage GetIconForText(string text, UIImage baseImage)
        {
            var nsText = new NSString(text);
            var icon   = iconCache.ObjectForKey(nsText);

            if (icon != null)
            {
                return((UIImage)icon);
            }

            var font = UIFont.BoldSystemFontOfSize(12);
            var size = baseImage.Size;

            UIGraphics.BeginImageContextWithOptions(size, false, 0.0f);
            baseImage.Draw(new CGRect(0, 0, size.Width, size.Height));
            var rect = new CGRect(0, 0, baseImage.Size.Width, baseImage.Size.Height);

            var paragraphStyle = NSParagraphStyle.Default;
            var attributes     = new UIStringAttributes(NSDictionary.FromObjectsAndKeys(
                                                            objects: new NSObject[] { font, paragraphStyle, options.RendererTextColor.ToUIColor() },
                                                            keys: new NSObject[] { UIStringAttributeKey.Font, UIStringAttributeKey.ParagraphStyle, UIStringAttributeKey.ForegroundColor }
                                                            ));

            var textSize = nsText.GetSizeUsingAttributes(attributes);
            var textRect = RectangleFExtensions.Inset(rect, (rect.Size.Width - textSize.Width) / 3,
                                                      (rect.Size.Height - textSize.Height) / 1);

            nsText.DrawString(RectangleFExtensions.Integral(textRect), attributes);

            var newImage = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            iconCache.SetObjectforKey(newImage, nsText);
            return(newImage);
        }
        UIImage FetchImageFromURL(NSUrl url)
        {
            var image = (UIImage)imageCache.ObjectForKey(url);

            if (image != null)
            {
                return(image);
            }
            else
            {
                // MARK: Do not cast the cache to Image as it will crash.
                // So, get a standard NSObject first and if it's not null then
                // cast to image.
                var urlObj = NSObject.FromObject(url.AbsoluteString);
                var obj    = networkImageRequested.ObjectForKey(urlObj);
                if (obj != null)
                {
                    return(null);
                }
                else
                {
                    image = (UIImage)obj;
                    networkImageRequested.SetObjectforKey(url, urlObj);
                }
            }

            NSData imageData = NSData.FromUrl(url);

            if (imageData == null)
            {
                return(null);
            }

            dataCache.SetObjectforKey(imageData, url);

            image = UIImage.LoadFromData(imageData);

            if (image != null)
            {
                imageCache.SetObjectforKey(image, url);
                return(image);
            }

            return(null);
        }
        public UIImage FetchThumbnailImageFromURL(NSUrl url)
        {
            var thumbnailImage = (UIImage)thumbnailImageCache.ObjectForKey(url);

            if (thumbnailImage == null)
            {
                UIImage image = FetchImageFromURL(url);
                if (image == null)
                {
                    return(null);
                }
                thumbnailImage = CreateThumbNailWithImage(image);
                if (thumbnailImage != null)
                {
                    thumbnailImageCache.SetObjectforKey(thumbnailImage, url);
                }
            }
            return(thumbnailImage);
        }
Пример #10
0
        DataItemViewController DataItemViewControllerForPage(int index)
        {
            var dataItem         = dataItems [index];
            var cachedController = dataItemViewControllerCache.ObjectForKey((NSString)dataItem.Identifier) as DataItemViewController;

            if (cachedController == null)
            {
                var viewController = (DataItemViewController)Storyboard.InstantiateViewController(DataItemViewController.StoryboardIdentifier);
                if (viewController == null)
                {
                    throw new Exception("Unable to instantiate a DataItemViewController.");
                }

                viewController.ConfigureWithDataItem(dataItem);
                dataItemViewControllerCache.SetObjectforKey(viewController, (NSString)dataItem.Identifier);
                return(viewController);
            }
            else
            {
                return(cachedController);
            }
        }
Пример #11
0
        public static UIImage GetImageBitmapFromWineId(string WineBarcode)
        {
            UIImage img = null;

            try
            {
                NSUrl  imageURL  = null;
                NSData imageData = null;
                imageData = ReadPhysicalCache(WineBarcode);
                if (imageData == null)
                {
                    url       = baseurl + "/barcodepp/" + WineBarcode;
                    imageURL  = new NSUrl(url);
                    imageData = NSData.FromUrl(imageURL);
                    CachedImagePhysically(imageData, WineBarcode);
                    img = UIImage.LoadFromData(imageData);
                }
                else
                {
                    img = UIImage.LoadFromData(imageData);
                    wineBottles.SetObjectforKey(img, NSObject.FromObject(WineBarcode));
                }
                if (img == null)
                {
                    img = new UIImage("Wines/bottle.jpg");
                }
            }
            catch (Exception e)
            {
                if (img == null)
                {
                    img = new UIImage("Wines/bottle.jpg");
                }
                //LoggingClass.LogError(e.Message, screen, e.StackTrace.ToString());
            }
            return(img);
        }
        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            UITableViewCell cell;

            if (chatMessageList[indexPath.Row].Type == 1)
            {
                cell           = tableView.DequeueReusableCell("MyImage", indexPath);
                cell.Transform = CGAffineTransform.MakeRotation((nfloat)Math.PI);

                var iv_MyImageView = (UIImageView)cell.ContentView.Subviews[0];
                iv_MyImageView.TranslatesAutoresizingMaskIntoConstraints = true;


                var DocUrl   = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                var FilePath = (NSString)System.IO.Path.Combine(DocUrl, chatMessageList[indexPath.Row].Message);
                try
                {
                    //從快取取出
                    var Image = (UIImage)imageCache.ObjectForKey(FilePath);
                    if (Image == null)
                    {
                        var ImageData = NSData.FromFile(FilePath);
                        Image = new UIImage(ImageData);
                        //存入快取
                        imageCache.SetObjectforKey(Image, FilePath);
                    }
                    nfloat ih;
                    rawValueDictionary.TryGetValue($"{chatMessageList[indexPath.Row].Message}ih", out ih);
                    if (ih == 0)
                    {
                        if (Image.Size.Width > contentMaxWidth)
                        {
                            rawValueDictionary[$"{chatMessageList[indexPath.Row].Message}ih"] = Image.Size.Height / (Image.Size.Width / contentMaxWidth);
                        }
                        else
                        {
                            rawValueDictionary[$"{chatMessageList[indexPath.Row].Message}ih"] = Image.Size.Height * (contentMaxWidth / Image.Size.Width);
                        }
                    }
                    var IVFrame = iv_MyImageView.Frame;
                    IVFrame.Width        = contentMaxWidth;
                    IVFrame.Height       = rawValueDictionary[$"{chatMessageList[indexPath.Row].Message}ih"];
                    IVFrame.X            = tableView.Frame.Width - contentMaxWidth - 10;
                    iv_MyImageView.Frame = IVFrame;

                    iv_MyImageView.Image = Image;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

                return(cell);
            }

            PaddingLabel l_Message;

            if (chatMessageList[indexPath.Row].Isme)
            {
                cell      = tableView.DequeueReusableCell("Right", indexPath);
                l_Message = (PaddingLabel)cell.ContentView.Subviews[0];
            }
            else
            {
                cell      = tableView.DequeueReusableCell("Left", indexPath);
                l_Message = (PaddingLabel)cell.ContentView.Subviews[2];
            }
            cell.Transform = CGAffineTransform.MakeRotation((nfloat)Math.PI);

            l_Message.TranslatesAutoresizingMaskIntoConstraints = true;
            l_Message.Text = chatMessageList[indexPath.Row].Message;

            nfloat lh;

            rawValueDictionary.TryGetValue($"{chatMessageList[indexPath.Row].Message}lh", out lh);
            if (lh == 0)
            {
                NSString tempText = (NSString)l_Message.Text;
                var      rect     = tempText.GetBoundingRect(
                    new CGSize(contentMaxWidth - 40, 1000),
                    NSStringDrawingOptions.UsesLineFragmentOrigin,
                    new UIStringAttributes()
                {
                    Font = l_Message.Font
                },
                    null);


                rawValueDictionary[$"{chatMessageList[indexPath.Row].Message}lh"] = rect.Height + 40;
                rawValueDictionary[$"{chatMessageList[indexPath.Row].Message}lw"] = rect.Width + 42;


                if (chatMessageList[indexPath.Row].Isme)
                {
                    rawValueDictionary[$"{chatMessageList[indexPath.Row].Message}lx"] = tableView.Frame.Width - rect.Width - 52;
                }
                else
                {
                    rawValueDictionary[$"{chatMessageList[indexPath.Row].Message}lx"] = cell.ContentView.Subviews[0].Frame.Width + 20;
                }
            }
            var Frame = l_Message.Frame;

            Frame.Height    = rawValueDictionary[$"{chatMessageList[indexPath.Row].Message}lh"];
            Frame.Width     = rawValueDictionary[$"{chatMessageList[indexPath.Row].Message}lw"];
            Frame.X         = rawValueDictionary[$"{chatMessageList[indexPath.Row].Message}lx"];
            l_Message.Frame = Frame;

            l_Message.Layer.CornerRadius  = 20;
            l_Message.Layer.MasksToBounds = true;


            if (!isLoading && !isFinishLoad && indexPath.Row > chatMessageList.Count * 0.7)
            {
                isLoading = true;
                ReadData();
            }

            return(cell);
        }