Skip to main content

polyhorn_ios_sys/polykit/
image_view.rs

1use objc::runtime::*;
2use objc::*;
3
4use super::PLYView;
5use crate::uikit::{UIColor, UIImage};
6use crate::Raw;
7
8/// An object that displays a single image or a sequence of animated images in
9/// your interface.
10pub struct PLYImageView {
11    pub(super) object: *mut Object,
12}
13
14impl PLYImageView {
15    /// Initializes a newly allocated image view.
16    pub fn new() -> PLYImageView {
17        unsafe {
18            let mut object: *mut Object = msg_send![class!(PLYImageView), alloc];
19            object = msg_send![object, init];
20            PLYImageView { object }
21        }
22    }
23
24    /// Sets the image displayed in the image view.
25    pub fn set_image(&mut self, image: &UIImage) {
26        unsafe {
27            let _: () = msg_send![self.object, setImage: image.as_raw()];
28        }
29    }
30
31    /// Tints the image displayed in the image view with the given color.
32    pub fn set_tint_color(&mut self, color: &UIColor) {
33        unsafe {
34            let _: () = msg_send![self.object, setTintColor: color.as_raw()];
35        }
36    }
37
38    /// Upcasts this view to a UIView.
39    pub fn to_view(&self) -> PLYView {
40        unsafe { PLYView::from_raw_retain(self.object) }
41    }
42}
43
44impl Raw for PLYImageView {
45    unsafe fn from_raw(object: *mut Object) -> Self {
46        PLYImageView { object }
47    }
48
49    unsafe fn as_raw(&self) -> *mut Object {
50        self.object
51    }
52}
53
54impl Drop for PLYImageView {
55    fn drop(&mut self) {
56        unsafe { objc_release(self.object) }
57    }
58}