I would like to implement my own subclass of ImageSource. Xamarin Forms has FileImageSource, UriImageSource and several other subclasses of ImageSource, but none of them meets my needs.
To see how to implement an ImageSource subclass, I looked at implementation of FileImageSource on github (https://github.com/xamarin/Xamarin.Forms/blob/5.0.0/Xamarin.Forms.Core/FileImageSource.cs)
I expected to find that the subclass would override one or more ImageSource methods to actually read the image file and provide the bytes from the file to the calling platform. But source code of FileImageSource contains no such thing! In fact, they use the File only to override ImageSource.IsEmpty. How can this be? If an image is contained in a file, you need to read the file to display this image, don't you?
What am I missing? And if I want to implement my own subclass of ImageSource, how can my sublass return image data to the platform?
Edit
Thank you to everyone who proposed solving the issue in a different way and inquiring why exactly existing ImageSource subclasses do not serve my needs.
Even if there is a different way to solve my immediate issue, from an educational perspective it is still interesting to know how Xamarin Forms displays an image file without reading the file. The source code for FileImageSource is quite short and does not appear to use the file, other than to implement IsEmpty. This will bug me until I know how this works.
[Resolve the issue using converters]
Instead of creating a new ImageSource, you could create a Converter (if you are working with bindings).
So if you have something like a byte array, and you want that to be shown as an image, you can do the conversion from byte array into a StreamImageSource
[EDIT: Knowing how it works]
If you want to know how it works internally and how it transforms the File into the actual Image, you will have to go deeper into the code, and check the platform implementations.
For example here you have the android renderers. You will find the IImageSourceHandler.cs and the FontImageSourceHandler and the FileImageSourceHandler and so on.
In swift 4 or even in previous version we can get the complete image though this code
ProfileImg.Image = marker.png
etc but In swift 4.2, we can only have
UIimage.init(named: "marker")
Is it a version mistake or removed by own.???
From the Xcode 10 beta release notes:
Code completion for Swift image literals was removed in Xcode 10. (38087260)
I imagine this was because the image literals were appearing in the completion list for everything and not just when the context involved UIImage, which was driving everybody crazy. Or maybe it's because the new editor can't accommodate the display of image literals.
The feature will either be restored in a future release or will be abandoned altogether. Time will tell!
When you started to write mar.. you ll see the picker that contains your image in the assets. You can pick from here directly.
Xcode 10 Release Notes: "Code Completion for Swift image literals has been removed in Xcode 10"
So it's not a bug that I couldn't add images in the code (and run older projects). How do we then add images from now on in the code as just typing UIImageView(image: won't accept any png or jpeg.
let cellImage: UIImageView = {
let image = UIImageView(image: ???))
return image
}()
System: macOS Mojave
Xcode: Xcode 10 Beta 4
In Xcode 10 and Swift 4.2, only the code completion function (or auto-complete) of the Xcode IDE has been discontinued for the old way. Here is the new way:
Just type image literal and it will complete with default icon.
Double click on this icon and it will open the media library right side of it.
Just choose your imagem and it work like before!
Found the Shortcut to open the Media Library Tab:
⇧+⌘+M
At this point the official documentation does not cover some use cases at Xcode 10 beta so I'm not able to find a way to move the media part to the old position.
You can use Media Library in Xcode to add image literal to your code:
And then choose image from library and drag&drop it to your code
It will create image literal
For the benefit of others with legacy code that utlizes Swift Image Literals, the code and syntax of the image literal function themselves are still valid and work perfectly fine in Xcode 10 with Swift 4.2.
There is no need to rollback or refactor existing code that utlizes these image literals, and they can still be used as follows:
imageView.image = #imageLiteral(resourceName: imageNameString)
Only the code completion function (or auto-complete) of the Xcode IDE has been discontinued.
UPDATE
The difference in image-literal representation in previous and current versions or Xcode/Swift:
image = #imageLiteral(resourceName: "flower.jpg")
As the extract illustrates, image literals were represented in-line with thumbnails of their actual image. However, now, only a generic icon is used in its place. Presumably, this goes towards reducing the burden on the IDE, by eliminating the overheads of handling and displaying the in-line thumbnails.
TIP: To fix three resolutions after add a literal image in code.
1. Remove another, keep only one image
2. Comment your line code with literal image, then will appear literal image in code, like: #imageLiteral(resourceName: "image#2x.png"
3. in resourceName value, remove #2x and file extension, like .png, keep only image name: #imageLiteral(resourceName: "image")
4. Uncomment! Image will be showed in code.
In Xcode 11, the media library is now under View > Show Library (SHIFT+CMD+L).
If anyone using Xcode 11 then you can find all media using below option:
Using
UIImage(imageLiteralResourceName: 'imageName')
won't return an optional and works just fine
Just use #imageLiteral(
It will work
i'm using Xcode 10 beta 4 and i have the same problem !
Resolved using the old way to pass an image
PizzaModel(
nome: "Marinara",
ingredienti: "Pomodoro, Aglio",
calorie: "729",
img: UIImage(named: "marinara")!
)
you can use this code in Xcode 14.1
"UIImageVievName".image = #imageLiteral(resourceName: "imageName")
and then you can click the photo icon, you can chose what you want to add the image.
Just type command+shift+L , you will get the option for image addition.
I'm not finding much documentation on embeding images in .as files. Really, I'd like to have some theory on it. So far from what I got reading here and there:
I placed an image in Assets folder inside src. Then right-clicked the image and clicked "Generate embed code", then this code line appears where the cursor was [Embed(source="fluffybunny.png")] what now? How do I assign it to a variable or something... I really didn't find it out there.
Instead of given object using .graphic atribbutes I want to use an image.
Also, does it have to be an .SWF?
There are quite a few resources on this (when you search for "as3 use embed tag"). Some of them are really helpful:
http://www.bit-101.com/blog/?p=853
http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf680e1-7ffe.html
The most basic thing is that you declare a variable of some type. And you use the [Embed] tag before the variable declaration. It's some kind of weird association. Something like:
[Embed(source="image.jpg")]
public var imageClass:Bitmap;
// later on you can instantiate it and use:
addChild(new imageClass()); // creates and adds new image
This is just a sample - there are a lot of types and ways to do it - give a shot the Adobe reference, there are tons of samples.
I am looking for a method exactly like -[NSWorkspace iconForFile:] but which returns the icon in a higher resolution if possible. In particular, I have an app which makes use of QuickLook to display previews of files, and I'd like it to fall back to the file icon if no quick look plugin is available. Using the iconForFile: method, however, yields a small 32x32 icon. Is there a better method around? One that returns an NSImage or CGImageRef is preferred, but less accessible methods might be fine too.
The returned image of -[NSWorkspace iconForFile:] contains multiple representations, including higher resolution ones.
If you try drawing it at 512x512 it will automatically pick the appropriate representation.
Here is the way to make icon bigger:
NSImage * icon = [NSWorkspace iconForFile:yourPath];
[icon setSize:NSMakeSize(64,64)];
Thats it. Good luck!