UIImageView
UImageView is a container, by using it's object you can display a image or sequence of images on user interface. The image will be set using UIImage class object which supports different formate like JPEG, png.
You can see the UIImageView's inheritance hierarchy below
NSObject
UIResponder
UIView
UIImageView
Let's see how to creating UIImageView object programmatically, to do that create new xcode project. After creating new project add images to the Assets.xcassets, the image should be of @2x,@3x.
Don't confuse about @2x and @3x, actually they image sizes according to mobile device resolutions, i.e., @2x is 100x100 the @3x will be 200x200.
Open ViewController class and replace viewDidLoad with the below code
UImageView is a container, by using it's object you can display a image or sequence of images on user interface. The image will be set using UIImage class object which supports different formate like JPEG, png.
You can see the UIImageView's inheritance hierarchy below
NSObject
UIResponder
UIView
UIImageView
Let's see how to creating UIImageView object programmatically, to do that create new xcode project. After creating new project add images to the Assets.xcassets, the image should be of @2x,@3x.
Don't confuse about @2x and @3x, actually they image sizes according to mobile device resolutions, i.e., @2x is 100x100 the @3x will be 200x200.
Open ViewController class and replace viewDidLoad with the below code
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor(colorLiteralRed: 0.1, green: 0.3, blue: 0.3, alpha: 1)
// allocating&initializing imageview object and specifying the content area by frame
let imageView = UIImageView(frame: CGRect(x:((self.view.frame.width/2.0)-50.0), y:100.0, width:100.0, height:100.0))
// setting up image to be displayed
imageView.image = UIImage(named: "profile")
// setting up highlighted image to be displayed when you change highlighted property to true
imageView.highlightedImage = UIImage(named: "3D_apple_logo")
// setting up view content mode, where the image actually shown based on the contentMode proprty
/*
public enum UIViewContentMode : Int {
case ScaleToFill
case ScaleAspectFit // contents scaled to fit with fixed aspect. remainder is transparent
case ScaleAspectFill // contents scaled to fill with fixed aspect. some portion of content may be clipped.
case Redraw // redraw on bounds change (calls -setNeedsDisplay)
case Center // contents remain same size. positioned adjusted.
case Top
case Bottom
case Left
case Right
case TopLeft
case TopRight
case BottomLeft
case BottomRight
}
*/
imageView.contentMode = .scaleAspectFit
// make sure this property to true when you use contentMode as .Center
imageView.clipsToBounds = true
//adding imageview to it's superview
self.view.addSubview(imageView)
//adding tap action to the imageview object
//if you want to add action to imageview object you need to enable the userInteractionEnabled property - true
imageView.isUserInteractionEnabled = true
//tap gesture
let tapAction = UITapGestureRecognizer(target: self, action: #selector(changeImage(_:)))
imageView.addGestureRecognizer(tapAction)
//making rounded image
imageView.layer.cornerRadius = imageView.frame.size.width/2
let imageView1 = UIImageView(frame: CGRect(x:(self.view.frame.width/2)-50, y:400, width:100, height:100))
imageView1.image = UIImage(named: "profile")
self.view.addSubview(imageView1)
imageView1.layer.cornerRadius = 10
//displaying animated images
let loaderImageView = UIImageView(frame: CGRect(x:(self.view.frame.width/2)-20, y:300, width:40, height:40))
let imageNames = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"];
var sequenceOfImages = [UIImage]()
for name in imageNames{
sequenceOfImages.append(UIImage(named: name)!)
}
loaderImageView.animationImages = sequenceOfImages
self.view.addSubview(loaderImageView)
loaderImageView.startAnimating()
}
func changeImage(_ gesture : UITapGestureRecognizer) -> Void {
let imageView = gesture.view as! UIImageView
if imageView.isHighlighted {
imageView.isHighlighted = false
}else{
imageView.isHighlighted = true
}
}
The whole code is about how to display a simple image, animated image and how to make rounded imageView. You can download source code from here.
0 Comments