public override void AwakeFromNib() { // Create a PHFetchResult object for each section in the table view. var allPhotosOptions = new PHFetchOptions(); allPhotosOptions.SortDescriptors = new [] { new NSSortDescriptor("creationDate", true) }; PHFetchResult allPhotos = PHAsset.FetchAssets(allPhotosOptions); PHFetchResult smartAlbums = PHAssetCollection.FetchAssetCollections(PHAssetCollectionType.SmartAlbum, PHAssetCollectionSubtype.AlbumRegular, null); PHFetchResult topLevelUserCollections = PHCollection.FetchTopLevelUserCollections(null); // Store the PHFetchResult objects and localized titles for each section. sectionFetchResults = new [] { allPhotos, smartAlbums, topLevelUserCollections }; sectionLocalizedTitles = new [] { string.Empty, "Smart Albums", "Albums", string.Empty }; PHPhotoLibrary.SharedPhotoLibrary.RegisterChangeObserver(this); }
private void ImagePicker_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e) { if (sender is UIImagePickerController picker) { string fileName = null; if (e.Info.TryGetValue(UIImagePickerController.ReferenceUrl, out NSObject urlObj)) { var result = PHAsset.FetchAssets(new NSUrl[] { (urlObj as NSUrl) }, null); fileName = result?.firstObject?.ValueForKey(new NSString("filename"))?.ToString(); } fileName = fileName ?? $"photo_{DateTime.UtcNow.ToString("yyyyMMddHHmmss")}.jpg"; var lowerFilename = fileName?.ToLowerInvariant(); byte[] data; if (lowerFilename != null && (lowerFilename.EndsWith(".jpg") || lowerFilename.EndsWith(".jpeg"))) { using (var imageData = e.OriginalImage.AsJPEG()) { data = new byte[imageData.Length]; System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, data, 0, Convert.ToInt32(imageData.Length)); } } else { using (var imageData = e.OriginalImage.AsPNG()) { data = new byte[imageData.Length]; System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, data, 0, Convert.ToInt32(imageData.Length)); } } SelectFileResult(data, fileName); picker.DismissViewController(true, null); } }
public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender) { if (!(segue.DestinationViewController is AssetGridViewController) || !(sender is UITableViewCell)) { return; } var assetGridViewController = (AssetGridViewController)segue.DestinationViewController; var cell = (UITableViewCell)sender; // Set the title of the AssetGridViewController. assetGridViewController.Title = cell.TextLabel.Text; // Get the PHFetchResult for the selected section. NSIndexPath indexPath = TableView.IndexPathForCell(cell); PHFetchResult fetchResult = sectionFetchResults [indexPath.Section]; if (segue.Identifier == allPhotosSegue) { assetGridViewController.AssetsFetchResults = fetchResult; } else if (segue.Identifier == collectionSegue) { // Get the PHAssetCollection for the selected row. var collection = fetchResult [indexPath.Row] as PHAssetCollection; if (collection == null) { return; } var assetsFetchResult = PHAsset.FetchAssets(collection, null); assetGridViewController.AssetsFetchResults = assetsFetchResult; assetGridViewController.AssetCollection = collection; } }
private void LastTakenPhotoFunction() { if (!UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary)) { alertView = new UIAlertView("SnagR Upload Photos APP", "Sorry, you cannot select pictures with your device", new UIAlertViewDelegate(), "OK"); alertView.Show(); return; } //imagePicker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary; //ALAssetsLibrary library = new ALAssetsLibrary(); PHFetchOptions fetchOptions = new PHFetchOptions(); fetchOptions.SortDescriptors = new NSSortDescriptor[] { new NSSortDescriptor("creationDate", false) }; PHFetchResult fetchResult = PHAsset.FetchAssets(PHAssetMediaType.Image, fetchOptions); PHAsset lastAsset = (Photos.PHAsset)fetchResult.LastObject; PHImageRequestOptions option = new PHImageRequestOptions(); option.ResizeMode = PHImageRequestOptionsResizeMode.Exact; PHImageManager.DefaultManager.RequestImageData(lastAsset, option, (data, dataUti, orientation, info) => nsSelectedImage = data); //PHImageManager.DefaultManager.RequestImageForAsset(lastAsset, new CGSize(100, 100), PHImageContentMode.AspectFill, options:option,(UIImage result, NSDictionary info) => {selectedImage = result}); selectedImage = UIImage.LoadFromData(nsSelectedImage); TestImagePlace.Image = selectedImage; //imagePicker.AssetsLibrary UploadProcess(); }
public async Task <List <string> > GetGalleryPhotosAsync(int photoCount) { var photoAssets = new List <string>(); var fetchOptions = new PHFetchOptions { SortDescriptors = new NSSortDescriptor[] { new NSSortDescriptor("creationDate", false) }, }; var fetchAssetsResult = PHAsset.FetchAssets(PHAssetMediaType.Image, fetchOptions); var phAssetIndex = 0; var numPhotos = fetchAssetsResult.Count; while (phAssetIndex < numPhotos && phAssetIndex < photoCount) { var phAsset = (PHAsset)fetchAssetsResult.ObjectAt(phAssetIndex); var filePath = await GetFilePathFromAssetAsync(phAsset); photoAssets.Add(filePath); phAssetIndex++; } return(photoAssets); }
private async void SetGalleryButton() { var status = await PHPhotoLibrary.RequestAuthorizationAsync(); if (status == PHAuthorizationStatus.Authorized) { var options = new PHFetchOptions { SortDescriptors = new[] { new NSSortDescriptor("creationDate", false) }, FetchLimit = 5, }; var fetchedAssets = PHAsset.FetchAssets(PHAssetMediaType.Image, options); var lastGalleryPhoto = fetchedAssets.FirstOrDefault() as PHAsset; if (lastGalleryPhoto != null) { var PHImageManager = new PHImageManager(); PHImageManager.RequestImageForAsset(lastGalleryPhoto, new CGSize(300, 300), PHImageContentMode.AspectFill, new PHImageRequestOptions(), (img, info) => { galleryButton.Image = img; }); } else { galleryButton.Image = UIImage.FromBundle("ic_noavatar"); } } }
// TODO MAKE ASYNC public List <ImageMediaContent> loadImages() { List <ImageMediaContent> imageMedia = new List <ImageMediaContent>(); PHFetchResult fetchResult = PHAsset.FetchAssets(PHAssetMediaType.Image, null); nint index = 0; foreach (PHAsset phAsset in fetchResult) { String filePath = ""; UIImage image; imageManager.RequestImageForAsset((PHAsset)imageFetchResults[index], new SizeF(160, 160), PHImageContentMode.AspectFill, new PHImageRequestOptions(), (img, info) => { image = img; }); index++; phAsset.RequestContentEditingInput(new PHContentEditingInputRequestOptions(), (input, v) => { filePath = input.FullSizeImageUrl.Path; }); imageMedia.Add(new ImageMediaContent(filePath)); } return(imageMedia); }
private Task <OperationResult <LibraryMediaItem> > GetLastSavedMediaItem(MediaItemType itemType) { var tcs = new TaskCompletionSource <OperationResult <LibraryMediaItem> >(); var fetchOptions = new PHFetchOptions(); fetchOptions.SortDescriptors = new NSSortDescriptor[] { new NSSortDescriptor("creationDate", false) }; var fetchResult = PHAsset.FetchAssets(itemType == MediaItemType.Video ? PHAssetMediaType.Video : PHAssetMediaType.Image, fetchOptions); var phAsset = fetchResult?.firstObject as PHAsset; if (phAsset != null) { PHImageManager.DefaultManager.RequestAvAsset(phAsset, null, (asset, audioMix, info) => { var urlAsset = asset as AVUrlAsset; tcs.SetResult(OperationResult <LibraryMediaItem> .AsSuccess(new LibraryMediaItem(phAsset.LocalIdentifier, urlAsset.Url.AbsoluteString))); }); } else { tcs.SetResult(OperationResult <LibraryMediaItem> .AsFailure("Could not retrieve last asset")); } return(tcs.Task); }
void RetrieveLastAssetSaved() { var options = new PHFetchOptions(); options.SortDescriptors = new NSSortDescriptor[] { new NSSortDescriptor("creationDate", false) }; options.FetchLimit = 1; var result = PHAsset.FetchAssets(PHAssetMediaType.Video, options); if (result.firstObject != null) { var videoAsset = result.firstObject as PHAsset; if (videoAsset != null) { PHImageManager.DefaultManager.RequestAvAsset(videoAsset, null, (asset, audioMix, info) => { InvokeOnMainThread(() => { FinalizeSave(((AVUrlAsset)asset).Url.AbsoluteString); }); }); } else { Debug.WriteLine("FAIL"); //BTProgressHUD.Dismiss(); } } }
public void FetchResultIndex() { if (!TestRuntime.CheckSystemAndSDKVersion(8, 0)) { Assert.Inconclusive("Requires iOS8 or later"); } if (ALAssetsLibrary.AuthorizationStatus != ALAuthorizationStatus.Authorized) { Assert.Inconclusive("Requires access to the photo library"); } var collection = PHAsset.FetchAssets(PHAssetMediaType.Image, null); if (collection.Count == 0) { XamagramImage.Image.SaveToPhotosAlbum(null); collection = PHAsset.FetchAssets(PHAssetMediaType.Image, null); } // Actual Test var obj = collection [0]; Assert.IsNotNull(obj); }
private async void SetGalleryButton() { var status = await PHPhotoLibrary.RequestAuthorizationAsync(); if (status == PHAuthorizationStatus.Authorized) { var fetchedAssets = PHAsset.FetchAssets(PHAssetMediaType.Image, null); var lastGalleryPhoto = fetchedAssets.LastObject as PHAsset; if (lastGalleryPhoto != null) { galleryButton.UserInteractionEnabled = true; var PHImageManager = new PHImageManager(); PHImageManager.RequestImageForAsset(lastGalleryPhoto, new CGSize(300, 300), PHImageContentMode.AspectFill, new PHImageRequestOptions() { DeliveryMode = PHImageRequestOptionsDeliveryMode.Opportunistic, ResizeMode = PHImageRequestOptionsResizeMode.Exact }, (img, info) => { galleryButton.Image = img; }); } else { galleryButton.UserInteractionEnabled = false; } } else { galleryButton.UserInteractionEnabled = true; } }
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath) { var cell = collectionView.DequeueReusableCell(AlbumCollectionViewCell.ReuseIdentifier, indexPath) as AlbumCollectionViewCell; if (cell == null) { throw new InvalidProgramException("Unable to dequeue an AlbumCollectionViewCell"); } var collection = albums [indexPath.Row]; cell.ImageView.Image = null; cell.Label.Text = collection.LocalizedTitle; var firstAsset = (PHAsset)PHAsset.FetchAssets(collection, new PHFetchOptions()).firstObject; if (firstAsset != null) { var options = new PHImageRequestOptions { Synchronous = true }; PHImageManager.DefaultManager.RequestImageForAsset(firstAsset, cell.Bounds.Size, PHImageContentMode.AspectFit, options, (requestedImage, _) => { cell.ImageView.Image = requestedImage; }); } return(cell); }
public void FetchResultObjectsAt() { if (!TestRuntime.CheckSystemAndSDKVersion(8, 0)) { Assert.Inconclusive("Requires iOS8 or later"); } if (ALAssetsLibrary.AuthorizationStatus != ALAuthorizationStatus.Authorized) { Assert.Inconclusive("Requires access to the photo library"); } var collection = PHAsset.FetchAssets(PHAssetMediaType.Image, null); if (collection.Count == 0) { XamagramImage.Image.SaveToPhotosAlbum(null); collection = PHAsset.FetchAssets(PHAssetMediaType.Image, null); } // Actual Test var obj = collection.ObjectsAt <NSObject> (NSIndexSet.FromNSRange(new NSRange(0, 1))); Assert.That(obj != null && obj.Count() > 0); }
public iOSPhotoLibraryChangeObserver(iOSImageMetadataProbe probe, DispatchQueue dispatchQueue) { _probe = probe; _dispatchQueue = dispatchQueue; _imageFetchResult = PHAsset.FetchAssets(PHAssetMediaType.Image, new PHFetchOptions()); _videoFetchResult = PHAsset.FetchAssets(PHAssetMediaType.Video, new PHFetchOptions()); }
private Task GetImagesAssets() { this.imagesResult.Clear(); var assets = PHAsset.FetchAssets(PHAssetMediaType.Image, new PHFetchOptions()); assets.Enumerate(new PHFetchResultEnumerator(OnPHFetchResultEnumerator)); return(Task.FromResult(true)); }
private void FeetchAddPhotos() { PHPhotoLibrary.RequestAuthorization(status => { if (status != PHAuthorizationStatus.Authorized) { return; } var galleryTemp = new List <PHAssetCollection>(); var allAlbums = PHAssetCollection.FetchAssetCollections(PHAssetCollectionType.Album, PHAssetCollectionSubtype.Any, null).Cast <PHAssetCollection>(); var smartAlbums = PHAssetCollection.FetchAssetCollections(PHAssetCollectionType.SmartAlbum, PHAssetCollectionSubtype.SmartAlbumUserLibrary, null).Cast <PHAssetCollection>(); galleryTemp.AddRange(allAlbums); galleryTemp.AddRange(smartAlbums); var gallerySort = galleryTemp.OrderBy(obj => obj.LocalizedTitle); NSOperationQueue.MainQueue.AddOperation(() => { foreach (var itemRaw in gallerySort) { var sortOptions = new PHFetchOptions(); sortOptions.SortDescriptors = new NSSortDescriptor[] { new NSSortDescriptor("creationDate", false) }; var items = PHAsset.FetchAssets(itemRaw, sortOptions).Cast <PHAsset>().ToList(); if (items.Count > 0) { var colec = new GalleryNative() { Collection = itemRaw, }; colec.Images.Add(new PhotoSetNative()); foreach (var item in items) { var newPhoto = new PhotoSetNative(); newPhoto.galleryImageXF.OriginalPath = item.LocalIdentifier; newPhoto.Image = item; colec.Images.Add(newPhoto); } galleryDirectories.Add(colec); } } tableView.ReloadData(); if (galleryDirectories.Count > 0) { CurrentParent = 0; IF_ItemSelectd(CurrentParent); } }); }); }
private PHFetchResult PrepareResults() { var fetchOptions = new PHFetchOptions() { SortDescriptors = new NSSortDescriptor[] { new NSSortDescriptor("creationDate", true) } }; return(PHAsset.FetchAssets(PHAssetMediaType.Image, fetchOptions)); }
public PhotoCollectionViewSource() { PHFetchOptions options = new PHFetchOptions { SortDescriptors = new[] { new NSSortDescriptor("creationDate", false) }, }; _fetchResults = PHAsset.FetchAssets(PHAssetMediaType.Image, options); _m = new PHImageManager(); }
public PhotosViewController(UICollectionViewLayout layout) : base(layout) { Title = "All Photos"; imageMgr = new PHImageManager(); fetchResults = PHAsset.FetchAssets(PHAssetMediaType.Image, null); observer = new PhotoLibraryObserver(this); PHPhotoLibrary.SharedPhotoLibrary.RegisterChangeObserver(observer); }
private static PHAsset GetAssetPH(NSUrl assetUrl) { var assets = PHAsset.FetchAssets(new NSUrl[] { assetUrl }, null); if (assets.Count == 0) { throw new NullReferenceException("Unable to find the specified asset."); } var asset = (PHAsset)assets[0]; return(asset); }
private async Task <bool> Import() { var status = await PHPhotoLibrary.RequestAuthorizationAsync(); if (status != PHAuthorizationStatus.Authorized) { return(false); } results = PHAsset.FetchAssets(PHAssetMediaType.Image, null).Select(x => (PHAsset)x).ToArray(); return(true); }
public UILatestPhotosOverlayView(CGRect frame) : base(frame, new UIHorizontalScrollLayout()) { DataSource = this; RegisterClassForCell(typeof(UIImageViewCell), imageViewCellId); fetchResults = PHAsset.FetchAssets(PHAssetMediaType.Image, null); manager = new PHImageManager(); var tapRecognizer = new UITapGestureRecognizer(() => Console.Write("test")); AddGestureRecognizer(tapRecognizer); TranslatesAutoresizingMaskIntoConstraints = false; BackgroundColor = UIColor.Clear; }
void GetPhotos() { if (PHPhotoLibrary.AuthorizationStatus == PHAuthorizationStatus.Authorized) { imageManager = PHImageManager.DefaultManager; PHFetchOptions options = new PHFetchOptions { SortDescriptors = new [] { new NSSortDescriptor("creationDate", false) } }; images = PHAsset.FetchAssets(PHAssetMediaType.Image, options); CollectionView.ReloadData(); } }
public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender) { var destination = (segue.DestinationViewController as UINavigationController)?.TopViewController as AssetGridViewController; if (destination == null) { throw new InvalidProgramException("unexpected view controller for segue"); } var cell = sender as UITableViewCell; if (destination == null) { throw new InvalidProgramException("unexpected cell for segue"); } destination.Title = cell.TextLabel.Text; switch (segue.Identifier) { case showAllPhotos: destination.FetchResult = allPhotos; break; case showCollection: // get the asset collection for the selected row var indexPath = TableView.IndexPathForCell(cell); PHCollection collection = null; switch ((Section)indexPath.Section) { case Section.SmartAlbums: collection = (PHAssetCollection)smartAlbums.ObjectAt(indexPath.Row); break; case Section.UserCollections: collection = (PHCollection)userCollections.ObjectAt(indexPath.Row); break; } // configure the view controller with the asset collection var assetCollection = collection as PHAssetCollection; if (assetCollection == null) { throw new InvalidProgramException("expected asset collection"); } destination.FetchResult = PHAsset.FetchAssets(assetCollection, null); destination.AssetCollection = assetCollection; break; } }
public async Task GetAllThumbnails(List <ItemModel> items) { await Task.Run(() => { PHFetchResult fetchResults = PHAsset.FetchAssets(PHAssetMediaType.Image, null); foreach (PHAsset asset in fetchResults) { items.Add(new ItemModel { ThumbLoader = new ThumbLoader_iOS(asset), ModificationDate = asset.ModificationDate.ToDateTime() }); } }); }
/// <summary> /// /// </summary> /// <param name="url"></param> /// <returns></returns> public static NSDictionary GetPhotoLibraryMetadata(NSUrl url) { NSDictionary meta = null; var image = PHAsset.FetchAssets(new NSUrl[] { url }, new PHFetchOptions()).firstObject as PHAsset; if (image == null) { return(null); } try { var imageManager = PHImageManager.DefaultManager; var requestOptions = new PHImageRequestOptions { Synchronous = true, NetworkAccessAllowed = true, DeliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat, }; imageManager.RequestImageData(image, requestOptions, (data, dataUti, orientation, info) => { try { var fullimage = CIImage.FromData(data); if (fullimage?.Properties != null) { meta = new NSMutableDictionary { [ImageIO.CGImageProperties.Orientation] = NSNumber.FromNInt((int)(fullimage.Properties.Orientation ?? CIImageOrientation.TopLeft)), [ImageIO.CGImageProperties.ExifDictionary] = fullimage.Properties.Exif?.Dictionary ?? new NSDictionary(), [ImageIO.CGImageProperties.TIFFDictionary] = fullimage.Properties.Tiff?.Dictionary ?? new NSDictionary(), [ImageIO.CGImageProperties.GPSDictionary] = fullimage.Properties.Gps?.Dictionary ?? new NSDictionary(), [ImageIO.CGImageProperties.IPTCDictionary] = fullimage.Properties.Iptc?.Dictionary ?? new NSDictionary(), [ImageIO.CGImageProperties.JFIFDictionary] = fullimage.Properties.Jfif?.Dictionary ?? new NSDictionary() }; } } catch (Exception ex) { Console.WriteLine(ex); } }); } catch { } return(meta); }
public void FetchResultToArray() { var collection = PHAsset.FetchAssets(PHAssetMediaType.Image, null); if (collection.Count == 0) { XamagramImage.Image.SaveToPhotosAlbum(null); collection = PHAsset.FetchAssets(PHAssetMediaType.Image, null); } // Actual Test var array = collection.ToArray(); Assert.That(array != null && array.Count() > 0); }
public void FetchResultIndex() { var collection = PHAsset.FetchAssets(PHAssetMediaType.Image, null); if (collection.Count == 0) { XamagramImage.Image.SaveToPhotosAlbum(null); collection = PHAsset.FetchAssets(PHAssetMediaType.Image, null); } // Actual Test var obj = collection [0]; Assert.IsNotNull(obj); }
public void FetchResultObjectsAt() { var collection = PHAsset.FetchAssets(PHAssetMediaType.Image, null); if (collection.Count == 0) { XamagramImage.Image.SaveToPhotosAlbum(null); collection = PHAsset.FetchAssets(PHAssetMediaType.Image, null); } // Actual Test var obj = collection.ObjectsAt <NSObject> (NSIndexSet.FromNSRange(new NSRange(0, 1))); Assert.That(obj != null && obj.Count() > 0); }
private void LoadAssetsFromLibrary() { var assetsOptions = new PHFetchOptions(); // include all source types assetsOptions.IncludeAssetSourceTypes = PHAssetSourceType.CloudShared | PHAssetSourceType.UserLibrary | PHAssetSourceType.iTunesSynced; // show most recent first assetsOptions.SortDescriptors = new NSSortDescriptor[] { new NSSortDescriptor("modificationDate", false) }; // fecth videos this.assets = PHAsset.FetchAssets(PHAssetMediaType.Video, assetsOptions); // setup collection view this.RecalculateItemSize(); this.CollectionView?.ReloadData(); }