Is there any kind of restriction when accessing image through widgets extension?
I have an image within Widget extension Supporting Files group called pocket.png
I want to use this image through by Widget custom view controller and as usual i wrote this code
var pocketImage:UIImage = UIImage(named: "pocket")
To my surprise this code returns blank. Tried with pocket.png same problem.
Now i tried to do with image with path and even the below code returns nil
let pathx = NSBundle.mainBundle().pathForResource("pocket", ofType: "png")
So how do i access local available image?
Note: The same code works fine when i use it in the main application with its local image.
There was an issue with xcode which didn't copy the images properly. I had to delete and copy them again to fix the issue.
Related
When my .net maui app starts it loads some images from the cloud and stores to FileSystem.AppDataDirectory folder. Then in offline mode it supposed to take them from the folder to display in the Image view, but the code does not work (binding will be used, but for the demo sake it's hardcoded):
<Image WidthRequest="30" HeightRequest="30">
<Image.Source>
<FileImageSource File="/data/user/0/mytestapp/files/Icontest.svg" />
</Image.Source>
</Image>
Same image works fine if loaded from the cloud when I replace File source to Url source.
/data/user/0/mytestapp/files/ is what FileSystem.AppDataDirectory call returns, I just added file name to the end
Any idea if it's supported at all to load image files from the folder?
note: caching with url source does not serve the app requirements.
SVG get pre-rendered and ship with your app. You have to reference them as PNG in your XAML. (On IOS, it wont even display if you use .svg) But this is not it.
Once replace your SVG resources with PNG. You will hit another problem.
The android will cache your files in your file system, and there is nothing you can do about it: https://github.com/dotnet/maui/issues/9773
There are two work arounds:
Change the name of the file every time, so it is never found.
Clear the cache by deleting everything from here: Path.Combine(FileSystem.CacheDirectory, "image_manager_disk_cache");
I think the negative effects of both workarounds are obvious. Fingers crossed that it gets fixed next year.
At first, the Image control can't get the file from the device's external storage directly. The FileImageSource is used to get the image file in the project which is set as embedded resource.
So you can need to use a stream to read the file and then use the ImageSource.FromStream to set the image source.
Such as:
var memoryStream = new MemoryStream();
using (var source = System.IO.File.OpenRead(path))
{
source.CopyTo(memoryStream);
}
image.Source = ImageSource.FromStream(() => memoryStream)
But there is a bug about ImageSource is not work when set Image.Source to file in external storage. Even the image file has been read as a stream, the image will not display. You can follow up this issue on the github.
I have a xamarin forms application. With the following code I try to show an image:
var image = new Image { Source = "launcher_foreground.png" };
The problem is that it doesn't show the image. The image is included under Sources in the Android specific project. Also, even the icon.png isn't showing, the default image included in the Xamarin forms app. The images are also set to AndroidResource.
What might be the problem here?
1.Please make sure your image has been placed under Resources/drawable directory with Build Action: AndroidResource.
2.The code you using is right.
public MainPage()
{
InitializeComponent();
var image = new Image { Source = "launcher_foreground.png" };
Content = image;
}
3.You can't load the icon.png as it is not in the Resources/drawable directory and the icon is configured in the Android project.
4.High- and low-DPI versions of an image can also be supplied (in appropriately named Resources subdirectories such as drawable-ldpi, drawable-hdpi, and drawable-xhdpi).
There are examples in the document. Also try to clean/rebuild your solution if the image still does not show.
Refer: local-images
try this code snippet , may be help you.
var image = new Image { Source = ImageSource.FromFile("launcher_foreground.png")}
I have the following Image asset defined (FSDirectButton)...
How do I specify a button to use this as it's image in the Xamarin xib designer?
When I choose the Image option in the properties window all I see are images that are stored in my Images folder.
I inherited this app which was originally done in a older version of Xamarin iOS, so not sure if that is affecting things or not...
However I want to use the image asset library for my buttons, but I am not sure how to specify in the designer which image to use? Or do I have to do it runtime in code?
Look at this video. It can help you, but raise some new bug. Enjoy
Also you can set the Image from Asset catalog in your ViewDidLoad() method.
For example:
//get image set from asset catalog
var imageFromBunde = UIImage.FromBundle("ImageSet"); //ImageSet - name of your image set in Asset catalog
myImage.Image = imageFromBundle;
While developing a project for Windows Phone 7, I'm adding an image control onto the Grid and using the following code in C# to set the image source:-
Uri nUri = new Uri("/TestImage.png", UriKind.Relative);
BitmapImage nBitmapImage = new BitmapImage(nUri);
FacebookImage.Source = nBitmapImage;
The problem is that whenever I use custom images, they do not show up when I build and run the app on the emulator. Whereas, if I change the image file name in the above code to one of the images that are included in the project by default (ApplicationIcon.png), the image shows up upon running. I would like to know if this is a problem with the code or with the custom image files I'm using. I'm ensuring that the images I'm creating are proper and have the same attributes as the images included by default.
Could someone please help me out with this.
Thanks.
Click on your custom picture in Visual Studio's Solution Explorer. Then, look at the property window (press F4 if it's closed), and at the "Build Action" line. It basically tells the compiler how to embed your file in the application. The build action of ApplicationIcon.png should be "Content", just set the same one for your custom pictures.
In my Silverlight project, images for which the source URI does not contain the file extension don`t display, although the documentation says it should.
I set the image source like so:
imgCompanyLogo.Source = new BitmapImage(new Uri(Application.Current.Host.Source, "/Files/" + logoName));
Now, if "logoName" contains the file extension (like ".png" for example), the image is displayed fine, but it simply doesn't if the file is stored without an extension.
This seems to contradict the documentation here which states:
"The format-specific filename extensions such as .png are not necessarily required to be in the URI naming, but if the retrieved file is not determined to be a valid image format, a runtime exception is thrown."
I'm not getting any runtime exception either.
Is this a known issue or am I missing something simple?
Thanks!
PS: Just a little twist, the images display fine while debugging, not when the system is deployed...
I've made some test and the problem seems to be due to the response from the server.
If you try using .png within your project with a Build Action sets to Resource, both images will load regardless the extension.
Now if you try with images hosted on a server, it won't have the same behavior. Actually, if you try to browse the link to an image without extension directly in your browser, it will result in something else. On Chrome it will download the file, and on IE it will display the result as plain text.
That's because of the MIME type. A png should be returned with the type image\png.
There is a trick with .htaccess to set up the MIME-type but you need to have to specify for which extension. It works like this:
AddType image\png yourExtension [Extension2] [Extension3] ..
And if you want to see if why the image didn't load on your Image control, you can add an eventhandler to the ImageFailed event:
<Image Source="..." ImageFailed="Image_ImageFailed" />
But the error message that you will see is not really helpful:
ErrorException = {System.Exception: AG_E_NETWORK_ERROR}