Android FFImageLoading using URI (Xamarin)

给你一囗甜甜゛ 提交于 2019-12-07 16:12:15

问题


I am trying to load the contact images from a cursor, so I have the URI of each image.

But I would like to use the FFImageLoading library to add these images to the view, so that I can easily load placeholders and do a circle transform.

However, I am having difficulty using the library with a URI - I have tried converting the URI to a url using the Path to use the LoadFromURL method, but that has been unsuccessful.

So I am wondering if it would be better to use the LoadImage or LoadStream methods, but I am unsure how best to do so.

Here is what I essentially want to do

    // use FFImageLoading library to asynchronously:
    await ImageService
        .Instance
        .LoadUrl(item.PhotoURL, TimeSpan.FromHours(Settings.ImageCacheDurationHours))  // get the image from a URL
        .LoadingPlaceholder("placeholderProfileImage.png")                                          // specify a placeholder image
        .Transform(new CircleTransformation())                                                      // transform the image to a circle
        .Error(e => System.Diagnostics.Debug.WriteLine(e.Message))
        .IntoAsync(viewHolder.ProfilePhotoImageView);

However, for the images I get from the contacts, I have a Uri and I can load it using the following, but I cant perform a transformation on it:

    var contactUri = ContentUris.WithAppendedId(ContactsContract.Contacts.ContentUri, Contacts[position].LongId);
    var contactPhotoUri = Android.Net.Uri.WithAppendedPath(contactUri, Android.Provider.Contacts.Photos.ContentDirectory);
    viewHolder.ProfilePhotoImageView.SetImageURI(contactPhotoUri);

Also, for relevance here is how I get the contacts:

    var uri = ContactsContract.Contacts.ContentUri;

    string[] projection = {
        ContactsContract.Contacts.InterfaceConsts.Id,
        ContactsContract.Contacts.InterfaceConsts.DisplayName,
        ContactsContract.Contacts.InterfaceConsts.PhotoId
    };

    // CursorLoader 
    var loader = new CursorLoader(activity, uri, projection, null, null, null);
    var cursor = (ICursor)loader.LoadInBackground();

    if (cursor.MoveToFirst())
    {
        do
        {
            Contacts.Add(new Contact
            {

                LongId = cursor.GetLong(cursor.GetColumnIndex(projection[0])),
                LastName = cursor.GetString(cursor.GetColumnIndex(projection[1])),
                PhotoUrl = cursor.GetString(cursor.GetColumnIndex(projection[2]))
            });
        } while (cursor.MoveToNext());
    }

来源:https://stackoverflow.com/questions/40738115/android-ffimageloading-using-uri-xamarin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!