我有一个应用程序,我需要从手机中提取图像,并按照用户选择的顺序显示它们(我需要img按照所选的顺序,而不是“最近编辑的”,这就是为什么我不使用UIImagePickerController)
要做到这一点,我有一个页面,显示最新的50个图片,有可能加载更多。
下面的代码将显示图像路径和方向。
我的问题是: CompletionHandler (在RequestContentEditingInput中)只运行17次.我可以看到,每个图像都需要调用RequestContentEditingInput (在这个电话上是138次)
我试过等待,但什么也没发生。
当我有不到17张图片的时候,代码就会正常工作.
我试图在SemaphoreSlim代码中实现一个CompletionHandler锁,以控制我当时只处理10个图像,但最终的结果是完全没有处理相同的图像。
UpdateInfo:似乎所有的LivePhotos都没有被处理。
有人知道这里发生了什么吗?
一个解决办法的主意?
还有别的方法可以得到所有的图像路径和方向吗?
或者一个完全不同的解决方案,让用户从手机中提取图像,并获得用户选择的顺序?
public class GalleryHelper : IGalleryHelper
{
private List<(string, string, string)> _retval;
private nint _numberofImg = 0;
private nint _currentImg = 0;
public List<(string, string, string)> GetAllImgFromGallery()
{
_retval = new List<(string, string, string)>();
_currentImg = 0;
GetAllImg();
return _retval;
}
private void GetAllImg()
{
var fetchOptions = new PHFetchOptions();
PHFetchResult allPhotos = PHAsset.FetchAssets(PHAssetMediaType.Image, fetchOptions);
_numberofImg = allPhotos.Count;
Debug.WriteLine("Total img no + " + _numberofImg);
for (nint i = 0; i < allPhotos.Count; i++)
{
Debug.WriteLine("Starting " + i);
(allPhotos[i] as PHAsset).RequestContentEditingInput(new PHContentEditingInputRequestOptions(), CompletionHandler);
}
}
private void CompletionHandler(PHContentEditingInput contentEditingInput, NSDictionary requestStatusInfo)
{
Debug.WriteLine("Starting CompletionHandler " + (1 + _currentImg));
var path = contentEditingInput.FullSizeImageUrl.Path;
if (path != null)
{
var orientation = contentEditingInput.FullSizeImageOrientation;
switch (orientation)
{
case CIImageOrientation.TopLeft:
//Standard position
_retval.Add((path, null, "0"));
break;
case CIImageOrientation.LeftBottom:
//Rotate 90 degrees clockwise
_retval.Add((path, null, "-90"));
break;
case CIImageOrientation.RightTop:
//Rotate 90 degrees counterclockwise
_retval.Add((path, null, "90"));
break;
case CIImageOrientation.BottomRight:
//Rotate 180 degrees
_retval.Add((path, null, "180"));
break;
case CIImageOrientation.BottomLeft:
//Mirror image rotated 180 degrees
_retval.Add((path, null, "180"));
break;
case CIImageOrientation.TopRight:
//Mirror image
_retval.Add((path, null, "0"));
break;
case CIImageOrientation.LeftTop:
//Mirror image rotate 90 degrees clockwise
_retval.Add((path, null, "-90"));
break;
case CIImageOrientation.RightBottom:
//Mirror image rotate 90 degrees counterclockwise.
_retval.Add((path, null, "90"));
break;
default:
_retval.Add((path, null, "0"));
break;
}
}
_currentImg++;
Debug.WriteLine("Images done " + _currentImg);
MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "ImagesReady", _retval);
}
}运行时来自我的调试输出的粘贴:
[0:] Total img no + 138
[0:] Starting 0
[0:] Starting 1
(...)
[0:] Starting 137
[0:] Starting CompletionHandler 1
[0:] Images done 1
[0:] Starting CompletionHandler 2
[0:] Images done 2
[0:] Starting CompletionHandler 3
[0:] Images done 3
(...)
[0:] Starting CompletionHandler 15
[0:] Images done 15
[0:] Starting CompletionHandler 16
[0:] Images done 16
[0:] Starting CompletionHandler 17
[0:] Images done 17
Thread started: #17
Thread finished: #15
The thread 0xf has exited with code 0 (0x0).
Thread finished: #3发布于 2018-12-03 09:30:47
如果将来有人有同样的问题。使用PHImageManager.DefaultManager.RequestImageData()而不是PHAsset.RequestContentEditingInput(),因为如果您要求它处理大量图像,它不会崩溃。
我所做的事情是:
var fetchOptions = new PHFetchOptions();
PHFetchResult allPhotos = PHAsset.FetchAssets(PHAssetMediaType.Image, fetchOptions);
_numberofImg = allPhotos.Count;
Debug.WriteLine("Total img no + " + _numberofImg);
await Task.Yield();
await Task.Run(() => ImgProcess(allPhotos));
return true;
private void ImgProcess(PHFetchResult allPhotos)
{
//await Task.Run(() =>
//{
for (nint i = 0; i < allPhotos.Count; i++)
{
Debug.WriteLine("Starting " + i);
var phasset = allPhotos[i] as PHAsset;
var options = new PHImageRequestOptions()
{
Synchronous = true,
NetworkAccessAllowed = false,
DeliveryMode = PHImageRequestOptionsDeliveryMode.FastFormat
};
PHImageManager.DefaultManager.RequestImageData(phasset, options, ComplManager);
}
//});
//return;
}即使这个解决方案仍然有问题,当图像的数量超过300.
https://stackoverflow.com/questions/53411696
复制相似问题