-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Android] Image.Source = ImageSource.FromResource() & ImageSource.FromStream() are not reliably integrating back with the MainThread #17857
Comments
I was able to reproduce it with about 180 lines of code. Posted repro project here: https://github.com/jonmdev/ImageSource-Bug This project just loads two random cat photos to screen each time you click the screen and resizes them. Image loading and resizing is managed with a small Within 1-5 clicks it will spew out the errors posted in the OP about Repro code, for reference, to replace App.xaml.cs:
|
For whatever it's worth, I wrote my own custom code using the handler and native views to load my images without the async function it was using by default (I really think we ought to have a choice about async vs. sync) and it's completely solved with just this method. My solution UIImageVIew loads UIImage. ImageVIew loads Android Bitmaps, and Windows loads BitmapImage. SKBitmap can be directly converted to Android Bitmap or UIImage by the Skiasharp.Views package. If you have an image loaded to a memory stream, this can be used to make a BitmapImage as well. Windows was the worst one and took me hours to figure out but I was able to make a non async memory stream and new BitmapImage to assign to the source without any async either. I wanted this anyway My suggestion
I might be missing something. But if it was me, I would make a "Generic bitmap" class with a variety of ways to load one. I would duplicate those methods into the Image class as well so people can bypass the "Generic bitmap" class when wanted and go straight into the Image. But this is just my opinion as I have been thinking about. It doesn't personally effect me as I understand the system well enough at least at this point to build exactly that for my own needs. If you are interested in improving the system, I think this should be a necessary step at some point. Decoupling the "Texture" or "Bitmap" from the "View" that holds it is a good step in most systems for design purposes. |
Yes @jstedfast, I just saw that user posting about this one asking for a backport and recognized it immediately as well. I linked his thread above. Thanks for your effort. Unrelated to the fix, just from having dug into the system a bit, I still think you guys should think about breaking the Image "View" class apart from the Image "Texture/Bitmap" and having a native "Texture/Bitmap" class (UIImage, Android BItmap, BitmapImage) but no big deal - bigger fish to fry. I can already write my own version for my own needs and I'm doing so. I just think having them separated out would improve the general usability of the system I think in many ways though for others. You can load and cache the image files, load them async or sync, set multiple Image Views to one image bitmap, etc. It gives a lot of options compared to marrying it to the image view itself and locking people just into async load. This bitmap/texture class could also carry the image height/width inside it for reference. I also think if you're going to stick with locked in async load there ought to at least be a "LoadCompleted" event or something for this people can subscribe to. Without that it's hard to build anything that responds correctly to the async. But this is not critical for me. I am writing my own code for all this. I just mention it for MAUI benefit or other users. Thanks again. Feel free to close this one then if it's solved. Thanks. |
Duplicate of #14052 |
Description
I am running the following code on a Behavior attached to an Image for managing the loading of photos to the Image. In Windows and iOS this can run 100+ times in a row with zero problems. I have never seen an error in them. But maybe 1 out of 5-7 times in Android fails with what appears to be a threading problem.
From what I can see in the Debugging,
Image.Source = ImageSource.FromResource()
runs an async process inside of it. This async process is not cooperating with the main thread in Android.There seems to be no way to load an Image without using this async process inside it which is causing the problems.
Here is my overly cautious code to try applying an ImageSource to an Image:
The Debug output confirms my command was run on the main thread as requested, and the command did not initially cause any errors. It debugs out the two Debug lines (one before and one after the
ImageSource
function) sequentially with nothing between and no problems.Yet a few lines of debugging later it then spits out an endless stream of errors. This to me suggests the
ImageSource
command is running async and then failing to re-integrate with the MainThread once it gets done or is trying to mess with the Image when it finishes out of turn.This is inconsistent but happens roughly 1/6 times. Most of the time it works. But once it breaks, the whole app gets glitchy.
I wonder if there is any way to create an Image entirely on the main thread. From what I see there is no single threaded option to circumvent this.
I also tried to use
FromStream
as follows but it caused the same problem, which makes sense if the issue is occurring at the end of the job not the start (how it is started makes no difference):If it matters, this is while attempting to load images listed in my project as "Embedded Resources" (not "MauiImage"). Again, I cannot see the same problem on Windows or iOS with the same code and I can't be any clearer about wanting this to main threaded.
Any thoughts or ideas?
Both issues only occur in Android and both resolve when I comment out the
imageView.Source = ...
Repro Project
https://github.com/jonmdev/ImageSource-Bug
Version with bug
7.0.92
Is this a regression from previous behavior?
No, this is something new
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android API 33, .NET 7.0
The text was updated successfully, but these errors were encountered: