ImageView
ImageView displays a UIng Image using a native image view. UIng accepts raw pixels and does not depend on an image decoder. Add CrImage to your application's shard.yml to use the optional image and color helpers:
dependencies:
uing:
github: kojix2/uing
crimage:
github: naqvis/crimage
require "uing/crimage"
source = CrImage.read("photo.png")
view = UIng::ImageView.new(source, :fit)
view.image = CrImage.read("next.png")
# For an Area, Table, or Toolbar, keep the native image until it is no longer used.
image = UIng::Image.from_crimage(source)
# ... use image ...
image.free
Runnable example
require "uing/crimage"
fname = File.join(__DIR__, "../gallery/crys.png")
source = CrImage.read(fname)
UIng.init
window = UIng::Window.new("ImageView Example", 300, 200, margined: true)
window.on_closing do
UIng.quit
true
end
vbox = UIng::Box.new(:vertical)
image_view = UIng::ImageView.new(source, :fit)
label = UIng::Label.new(fname)
vbox.append(image_view, stretchy: true)
vbox.append(label)
window.child = vbox
window.show
UIng.main
UIng.uninit
Usage notes
- Use
ContentMode::Fitto scale the image to fit the view, orContentMode::Centerto display it centered at its natural size. - Assign another Image with
image=, or assignnilto clear the view. - ImageView copies or retains its own native image. The source Image can therefore be freed immediately after construction or assignment.
require "uing"works without CrImage;require "uing/crimage"adds the optional helpers. CrImage must be a dependency of the application using them.UIng::Image.from_file(path)detects the format from file contents.CrImage.read(path)uses the filename extension in this CrImage version.UIng::Image.from_crimage,CrImage::Image#to_uing_image, andUIng::Image#append(CrImage::Image)accept any CrImage image. The adapter converts pixels to premultiplied RGBA and handles nonzero image origins and subimages.ImageViewreleases its temporary UIng image after assignment. Images returned byfrom_crimage,from_file, orto_uing_imagemust be freed by the caller after their consumers are done with them.- The same opt-in require adds CrImage color overloads for
ColorButton#set_color, solid brushes, gradient stops, text attributes, and table color values. UseCrImage::Color::NRGBAfor translucent UI colors. In the current CrImage version,Color.parsedoes not premultiply translucent RGBA correctly.