protected void RequestImageFromSource (string source, NIPhotoScrollViewPhotoSize photoSize, int photoIndex) { var isThumbnail = photoSize == NIPhotoScrollViewPhotoSize.NIPhotoScrollViewPhotoSizeThumbnail; var identifier = IdentifierWithPhotoSize (photoSize, photoIndex); var identifierKey = IdentifierKeyFromIdentifier (identifier); // avoid duplicate requests if (ActiveRequests.Contains (identifierKey)) return; NSUrl url = null; if (source.StartsWith ("http")) { url = new NSUrl (source); } else { url = new NSUrl (source, true); } NSMutableUrlRequest request = new NSMutableUrlRequest (url); request.TimeoutInterval = 30; var photoIndexKey = CacheKeyForPhotoIndex (photoIndex); var readOp = AFImageRequestOperation.ImageRequestOperationWithRequest (request, null, (NSUrlRequest req, NSHttpUrlResponse resp, UIImage img) => { // Store the image in the correct image cache. if (isThumbnail) { ThumbnailImageCache.StoreObject(img, photoIndexKey); } else { HighQualityImageCache.StoreObject(img, photoIndexKey); } // If you decide to move this code around then ensure that this method is called from // the main thread. Calling it from any other thread will have undefined results. PhotoAlbumView.DidLoadPhoto(img, photoIndex, photoSize); if(isThumbnail) { if(PhotoScrubberView != null) PhotoScrubberView.DidLoadThumbnail(img, photoIndex); } this.ActiveRequests.Remove(identifierKey); }, (NSUrlRequest req, NSHttpUrlResponse resp, NSError er) => { }); readOp.ImageScale = 1; // Set the operation priority level. if(photoSize == NIPhotoScrollViewPhotoSize.NIPhotoScrollViewPhotoSizeThumbnail) { readOp.QueuePriority = NSOperationQueuePriority.Low; }else { readOp.QueuePriority = NSOperationQueuePriority.Normal; } // Start the operation. ActiveRequests.Add(identifierKey); Queue.AddOperation(readOp); }
public override NSObject PhotoAlbumScrollView (NIPhotoAlbumScrollView photoAlbumScrollView, int photoIndex, out NIPhotoScrollViewPhotoSize photoSize, out bool isLoading, out SizeF originalPhotoDimensions) { isLoading = false; photoSize = NIPhotoScrollViewPhotoSize.NIPhotoScrollViewPhotoSizeUnknown; UIImage image; var photoIndexKey = _controller.CacheKeyForPhotoIndex (photoIndex); var photo = Photos [photoIndex]; originalPhotoDimensions = photo.Dimensions; image = (UIImage)_controller.HighQualityImageCache.ObjectWithName (photoIndexKey); if (image != null) { photoSize = NIPhotoScrollViewPhotoSize.NIPhotoScrollViewPhotoSizeOriginal; } else { var source = photo.OriginalSource; _controller.RequestImageFromSource (source, NIPhotoScrollViewPhotoSize.NIPhotoScrollViewPhotoSizeOriginal, photoIndex); isLoading = true; // try to load the thumbnail if we can image = (UIImage)_controller.ThumbnailImageCache.ObjectWithName (photoIndexKey); if (image != null) { photoSize = NIPhotoScrollViewPhotoSize.NIPhotoScrollViewPhotoSizeThumbnail; } else { _controller.RequestImageFromSource(photo.ThumbnailSoruce, NIPhotoScrollViewPhotoSize.NIPhotoScrollViewPhotoSizeThumbnail, photoIndex); } } return image; }
protected int IdentifierWithPhotoSize (NIPhotoScrollViewPhotoSize photoSize, int photoIndex) { var isThumbnail = photoSize == NIPhotoScrollViewPhotoSize.NIPhotoScrollViewPhotoSizeThumbnail; return isThumbnail ? -(photoIndex + 1) : photoIndex; }