我正在遵循这个blog 来从图库中选择多张图片。对于IOS,我正在使用GMImagePicker
从图库中选择多张图片。(在博客中建议使用elcimagepicker,但现在在Nuget Store中不可用)
我经历了GMImagePicker
usage 部分,但是没有找到如何将所选图像添加到List
并在MessagingCenter
中传递该值的方法(例如android实现)。在该用法部分中,仅讲述选择器设置。有人请给我任何使用此功能的示例代码吗?
Lucas Zhang - MSFT
,您好,我尝试了您的代码,但有一个问题。在这里,您仅通过messagecenter
传递一个文件路径,因此我应该使用列表发送多个文件路径吗?
我正在将图片路径作为string List
从android传递。请查看下面添加的android部件代码。我应该在IOS中这样做吗?
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok)
{
List<string> images = new List<string>();
if (data != null)
{
ClipData clipData = data.ClipData;
if (clipData != null)
{
for (int i = 0; i < clipData.ItemCount; i++)
{
ClipData.Item item = clipData.GetItemAt(i);
Android.Net.Uri uri = item.Uri;
var path = GetRealPathFromURI(uri);
if (path != null)
{
//Rotate Image
var imageRotated = ImageHelpers.RotateImage(path);
var newPath = ImageHelpers.SaveFile("TmpPictures", imageRotated, System.DateTime.Now.ToString("yyyyMMddHHmmssfff"));
images.Add(newPath);
}
}
}
else
{
Android.Net.Uri uri = data.Data;
var path = GetRealPathFromURI(uri);
if (path != null)
{
//Rotate Image
var imageRotated = ImageHelpers.RotateImage(path);
var newPath = ImageHelpers.SaveFile("TmpPictures", imageRotated, System.DateTime.Now.ToString("yyyyMMddHHmmssfff"));
images.Add(newPath);
}
}
MessagingCenter.Send<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelected", images);
}
}
}
我也遇到了错误,屏幕截图添加到下面:
答案 0 :(得分:0)
GMImagePicker 将返回包含import urllib.parse
def gen_uri_paths(baseURL, append_paths):
res = urllib.parse.urlparse(baseURL)
uri_paths = []
for p in append_paths:
u = list(res)
u[2] += p
uri_paths.append(urllib.parse.ParseResult(*u).geturl())
return uri_paths
# Returns ['https://example.com/p_1', 'https://example.com/p_2', 'https://example.com/p_3', 'https://example.com/a_1']
gen_uri_paths("https://example.com", ["p_1", "p_2", "p_3", "a_1"])
的列表。因此,您可以首先获取图像的filePath,然后使用PHAsset
和{{1}将它们传递到表单}。请参考以下代码。
在表单中,创建一个界面
MessagingCenter
在iOS项目中
DependencyService
在您的contentPages中
using System;
namespace app1
{
public interface ISelectMultiImage
{
void SelectedImage();
}
}
如果要选择图像,请调用方法
using System;
using Xamarin.Forms;
using UIKit;
using GMImagePicker;
using Photos;
using Foundation;
[assembly:Dependency(typeof(SelectMultiImageImplementation))]
namespace xxx.iOS
{
public class SelectMultiImageImplementation:ISelectMultiImage
{
public SelectMultiImageImplementation()
{
}
string Save(UIImage image, string name)
{
var documentsDirectory = Environment.GetFolderPath
(Environment.SpecialFolder.Personal);
string jpgFilename = System.IO.Path.Combine(documentsDirectory, name); // hardcoded filename, overwritten each time
NSData imgData = image.AsJPEG();
if (imgData.Save(jpgFilename, false, out NSError err))
{
return jpgFilename;
}
else
{
Console.WriteLine("NOT saved as " + jpgFilename + " because" + err.LocalizedDescription);
return null;
}
}
public void SelectedImage()
{
var picker = new GMImagePickerController();
picker.FinishedPickingAssets += (s, args) => {
PHAsset[] assets = args.Assets;
foreach (PHAsset asset in assets)
{
PHImageManager.DefaultManager.RequestImageData(asset, null, (NSData data, NSString dataUti, UIImageOrientation orientation, NSDictionary info) =>
{
NSUrl url = NSUrl.FromString(info.ValueForKey(new NSString("PHImageFileURLKey")).ToString());
string[] strs = url.Split("/");
UIImage image = UIImage.LoadFromData(data);
string file = Save(UIImage.LoadFromData(data), strs[strs.Length - 1]);
MessagingCenter.Send<Object, string>(this, "ImagesSelected", file);
}
);
}
};
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(picker, true,null);
}
}
}