Multiple Images Selection from gallery in .Net-IOS Solution.
Hi ,
I was using GMImagePicker Plugin in xamarin.ios solution to select multiple images from gallery. But while migrating the solution from xamarin.ios to .net-ios I found that GMImagePicker(A third party plugin) is not supporting .net sdk style project. Can anyone please suggest me good alternative for GMImagePicker , so that i can use to select multiple images from Gallery in .net-ios solution.
Looking forward for support and necessary information. Thank you !!
Xamarin
-
Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,391 Reputation points • Microsoft Vendor
2023-11-09T08:20:40.4166667+00:00 You can do this by PHPickerViewController | Apple Developer Documentation, apple provide A view controller that provides the user interface for choosing assets from the photo library.
-
SHEETAL RODRIGUES 20 Reputation points
2023-11-10T11:50:09.3+00:00 Hi,
I tried using PHPickerViewController. When I am trying to access assetIdentifier from PHPicker results ,I am getting AssetIdentifier as null in .net-ios solution.
-
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 32,306 Reputation points • Microsoft Vendor
2023-11-13T09:18:13.7+00:00 Do you set the
PHPickerViewControllerDelegate
for the picker and get the identifier like the following? Which device are you testing on? And what's the iOS version, MAUI version (.net-iOS version)?#if IOS public class CustomickerViewControllerDelegate : PHPickerViewControllerDelegate { public override void DidFinishPicking(PHPickerViewController picker, PHPickerResult[] results) { picker.DismissModalViewController(true); foreach (var item in results) { // item.AssetIdentifier won't be null var id = item.AssetIdentifier; } } } #endif
-
SHEETAL RODRIGUES 20 Reputation points
2023-11-13T11:03:13.54+00:00 Hi @**Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.)
**
yes, I followed the same method like you mentioned above. I am working on .net-ios 7.0 and testing in iphone 14 ios 16.4 Simulator.Since I was getting Asset Identifier as null , I tried other way to fetch the PHAsset from PHPickerResult. But here either NSData is not loading correctly from NSUrl or PHAsset from url.
Can you please confirm if PHPickerViewController supports .net-IOS 7.0 ? or if anything I am missing here.
if (results.Length > 0) { foreach (var result in results) { var assetIdentifier = result.AssetIdentifier; var itemProvider = result.ItemProvider; if (itemProvider.HasItemConformingTo("public.image")) { itemProvider.LoadItem("public.image", null, (NSObject data, NSError error) => { if (data is NSUrl url) { // Get the file name var filename = url.LastPathComponent; var nsData1 = new NSData(); // Load data from the URL try { nsData1 = NSData.FromUrl(url); } catch (Exception ex) { Console.WriteLine("Error fetching data from URL: " + ex.Message); return; } if (nsData1 == null) { Console.WriteLine("Error in fetching Data"); } if (nsData1 != null) { // Convert NSData to byte array byte[] imageData = new byte[nsData1.Length]; Marshal.Copy(nsData1.Bytes, imageData, 0, (int)nsData1.Length); // Save the image using the filename var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), filename); File.WriteAllBytes(filePath, imageData); // Use the filename or file path as needed Console.WriteLine("Selected image filename: " + filename); Console.WriteLine("Selected image file path: " + filePath); // Create a PHImageManager instance PHImageManager imageManager = new PHImageManager(); // Get the PHAsset from the NSUrl PHAsset asset = PHAsset.FetchAssets(new NSUrl[] { url }, null).FirstOrDefault() as PHAsset; //var image1 = new UIImage(filePath); NSData image2 = NSData.FromArray(imageData); var image3 = UIImage.LoadFromData(image2); } } }); } } }
-
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 32,306 Reputation points • Microsoft Vendor
2023-11-14T09:48:55.73+00:00 You mean the
assetIdentifier
is null when you get it from this linevar assetIdentifier = result.AssetIdentifier
, right? If so, theitemProvider
will benull
as well. And this causes all methods to get data to returnnull
.(The identifier is not null on my side)
Please try uninstalling the app and running again. Or try deploying on a physical device for testing.
You could also try adding using photo library keys.
<key>NSPhotoLibraryAddUsageDescription</key> <string>This app needs access to the photo gallery to save photos and videos.</string> <key>NSPhotoLibraryUsageDescription</key> <string>This app needs access to the photo gallery to save photos and videos.</string>
-
SHEETAL RODRIGUES 20 Reputation points
2023-11-20T15:45:55.5966667+00:00 Hi Wenyan,
Tried uninstalling the app and re-installing it. Also tried in physical device with adding above photo library keys. Still it is same. AssetIdentifier null
-
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 32,306 Reputation points • Microsoft Vendor
2023-11-22T09:11:45.6766667+00:00 You tested both Xamarin.iOS and .net-ios project, right? Does it work with Xamarin.iOS project?
(I tested the code snippets again, the AssetIdentifier is not null but the nsDsta is null)
Sign in to comment