Swiftui: What Are The Differences Between Image and UIimage

SwiftUI: What are the differences between Image and UIImage?

UIImage is a UIKit element it's created to be assigned to an UIImageView

Image is a SwiftUI element in the new framework released in IOS 13

You should avoid using UIKit elemnts in SwiftUI as possible , it's like using objective c code in swift which doesn't make sense of releasing a new language

Difference between UIImage and UIImageView

Example:

UIImage *bgImage = [UIImage imageNamed:@"Default@2x.png"];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:bgImage];
backgroundImageView.frame = [[UIScreen mainScreen] bounds];

UIImage Overview:

A UIImage object is a high-level way to display image data. You can
create images from files, from Quartz image objects, or from raw image
data you receive. The UIImage class also offers several options for
drawing images to the current graphics context using different blend
modes and opacity values.

Image objects are immutable, so you cannot change their properties
after creation. This means that you generally specify an image’s
properties at initialization time or rely on the image’s metadata to
provide the property value. In some cases, however, the UIImage class
provides convenience methods for obtaining a copy of the image that
uses custom values for a property.

Because image objects are immutable, they also do not provide direct
access to their underlying image data. However, you can get an NSData
object containing either a PNG or JPEG representation of the image
data using the UIImagePNGRepresentation and UIImageJPEGRepresentation
functions.

The system uses image objects to represent still pictures taken with
the camera on supported devices. To take a picture, use the
UIImagePickerController class. To save a picture to the Saved Photos
album, use the UIImageWriteToSavedPhotosAlbum function.

UIImageView Overview:

An UIImageView provides a view-based container for displaying
either a single image or for animating a series of images. For
animating the images, the UIImageView class provides controls to set
the duration and frequency of the animation. You can also start and
stop the animation freely.

New image view objects are configured to disregard user events by
default. If you want to handle events in a custom subclass of
UIImageView, you must explicitly change the value of the
userInteractionEnabled property to YES after initializing the object.

When a UIImageView object displays one of its images, the actual
behavior is based on the properties of the image and the view. If
either of the image’s leftCapWidth or topCapHeight properties are
non-zero, then the image is stretched according to the values in those
properties. Otherwise, the image is scaled, sized to fit, or
positioned in the image view according to the contentMode property of
the view. It is recommended (but not required) that you use images
that are all the same size. If the images are different sizes, each
will be adjusted to fit separately based on that mode.

All images associated with a UIImageView object should use the same
scale. If your application uses images with different scales, they may
render incorrectly.

How to compare two UIImage objects

One way is to convert them to image data first, and then compare that.

- (BOOL)image:(UIImage *)image1 isEqualTo:(UIImage *)image2
{
NSData *data1 = UIImagePNGRepresentation(image1);
NSData *data2 = UIImagePNGRepresentation(image2);

return [data1 isEqual:data2];
}

UIImage is equal to

You can convert your UIImage instances to NSData instances and compare them.

if let emptyImage = UIImage(named: "empty") {
let emptyData = UIImagePNGRepresentation(emptyImage)
let compareImageData = UIImagePNGRepresentation(image1.image)

if let empty = emptyData, compareTo = compareImageData {
if empty.isEqualToData(compareTo) {
// Empty image is the same as image1.image
} else {
// Empty image is not equal to image1.image
}
} else {
// Creating NSData from Images failed
}
}


Related Topics



Leave a reply



Submit