I was wondering how to create a UIImageView and set it’s image property and show it on the live view. This is Swift Playgrounds for iPad so there is no storyboards. I can’t add in a UIImageView unless I code it. How do I create a UIImageView in Swift Playgrounds for iPad?
let image = UIImage(named: “photo.png”)
let imageView = UIImageView()
imageView.image = image
view.addSubview(imageView)
imageView.Frame = CGRect(x: 0, y: 0, width: int, height: int)
This creates a custom frame. To have the frame match the image use imageView.image = UIImage(named: image) This is based of of rmaddy’s comment below
Related
I need to create custom button class, reuse it 4 times and I also need to override its text and image name. My next problem is how to set its frame somehow dynamically (now it is static), because I need this 4 buttons in grid 2x2.
I'm trying to create button exactly like this: https://imgur.com/a/dNhUGhc.
I have coded this but it is static and in ViewController I can't edit (override) these labels and image name. And if I tried to reuse this class I would have them in the same spot, because frame settings is exactly the same.
I'm subclassing UIButton. If something more suitable exists just let me know.
Code for adding label
// city label
let cityRect = CGRect(x: 0, y: 20, width: buttonWidth, height: 25)
let cityLabel = UILabel(frame: cityRect)
cityLabel.text = "Label"
cityLabel.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
cityLabel.textAlignment = .center
addSubview(cityLabel)
Code for adding image
// image
let imageView = UIImageView(image: UIImage(named: "something"))
imageView.frame = CGRect(x: 0, y: 60, width: 40, height: 40)
imageView.center.x = self.center.x - 20
addSubview(imageView)
Can you guys help me? Thanks
It looks like what you need to do is use an IBOutlet. Basically, an IBOutlet will give you a reference within your code (custom UIView or UIViewController subclass) to the button that you've setup in xib or storyboard. Then you can make any changes or adjustments that you want to it at runtime.
Check this out to learn more about IBOutlets and how to set them up in your project.
I just started working on Swift last week and i need a suggestion if the following approach is right ways of laying partial image on top of another image.
I have a UIView in which I am creating 3 images programmatically. Left arrow image, middle mobile image and right arrow image as shown below. Can I partially place arrow images 50% on the mobile image?
I have tried:
func setupUI(){
let mobileImage = UIImage(named: "mobile")
let arrowImage = UIImage(named: "arrow")
middleView = UIImageView(frame: CGRect(x: arrowImage!.size.width/2, y:0 , width:mobileImage!.size.width, height:mobileImage!.size.height))
middleView.image = mobileImage
middleView.layer.borderWidth = 1.0
middleView.layer.cornerRadius = 5.0
self.addSubview(middleView)
let yForArrow = mobileImage!.size.height - arrowImage!.size.height
leftArrow = UIImageView(frame: CGRect(x: 0, y:yForArrow, width:arrowImage!.size.width, height:arrowImage!.size.height))
leftArrow.image = arrowImage
self.addSubview(leftArrow)
let rightArrowX = mobileImage!.size.width
rightView = UIImageView(frame: CGRect(x: rightArrowX, y:yForArrow, width:arrowImage!.size.width, height:arrowImage!.size.height))
rightView.image = arrowImage
self.addSubview(rightView)
}
*At start it was not working, as i forgot to add setupUI() in init method. As shown in answer bellow.
Is setting frame correct way of doing it OR i should be using constraints?
To me it looks bad approach as i am hard coding the numbers in CGRect.
*This image is created in MS paint to show what it should look on iPhone.
I found the problem i missed adding setupUI() in init method.
SetupUI programatically adds images on UIView. As it was missing so no image was appearing in the iPhone simulator.
override init(frame: CGRect) {
super.init(frame: frame)
setupUI() // Code to add images in UIView
}
How can I add UIViewContentMode.center to this UIImageView while also keeping .scaleAspectFill?
func insertImage() {
let theImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 300))
theImageView.image = #imageLiteral(resourceName: "coolimage")
theImageView.contentMode = UIViewContentMode.scaleAspectFill
view.addSubview(theImageView)
}
Furthermore, can somebody explain to me what the "view" exactly is in the last line "view.addSubview(theImageView)"? Is it the mysterious "view hierarchy" that I read about? Why can't I simply initialize the UIImageView? Why must it be bound to something called "view" that I haven't explicitly created? There is only a UIViewController and a UIImageView so far.
As far as I know, you can't set content mode to both aspect fit and center. However, center will do what aspect fit does providing the image size is smaller than the size of the imageView. If not, use aspect fit. The following code ought to allow you to differentiate between the two:
if (theImageView.bounds.size.width > UIImage(named: "coolimage")?.size.width && theImageView.bounds.size.height > UIImage(named: "coolimage")?.size.height) {
theImageView.contentMode = .aspectFit
} else {
theImageView.contentMode = .center
}
As for the second part of your question, I'll refer you to this thread, which has a fairly comprehensive explanation of UIViewController vs UIView. Hope that helps.
Simple code for resizing image i.e. for navbar (source link) for png image with native res: 722x1028
let imageView = UIImageView(frame: CGRectMake(0, 0, 0, 60))
imageView.image = UIImage(named: "girl")
imageView.contentMode = UIViewContentMode.ScaleAspectFit
self.navigationItem.titleView = imageView
And I get on iPad2:
I can manually render desired image by changing code:
let imageView = UIImageView(frame: CGRectMake(0, 0, 0, 60))
imageView.image = imageWithImage(UIImage(named: "girl")!, scaledToSize: CGSizeMake(42,60))
self.navigationItem.titleView = imageView
func imageWithImage(image:UIImage, scaledToSize newSize:CGSize) -> UIImage{
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0);
image.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
But this is rather expensive solution.
Is there native library or simple swift solution to make png image automatically resized with good quality?
If you have hardcoded size 42x60 points for all devices, the best solution would be to provide pre-rendered image in that size. This is friendly to the battery and allows tweaking the image in a high quality editor before adding to the app: Resizing from 1000 pixels to 60 pixels is a bit drastic and will lead to loss of detail.
If that's not possible (i.e. image is dynamically loaded from the internet etc.), I would start my research with CIImage and CIFilter(name: "CILanczosScaleTransform") which provides very good interpolation quality, possibly followed by CISharpenLuminance if the loss of detail is too high.
i'm a new developer for ios. Is there a way where i could add a UIImageView on top of a UILabel ? I tried this but image is hidden from UILabel :(
var imageTick:UIImage = UIImage(named: "Trash-icon.png")
var imageView:UIImageView = UIImageView(frame: CGRectMake(self.bounds.size.width-50, self.bounds.size.height/2, 30, 30))
imageView.image = imageTick
labelTick.addSubview(imageView)
self.addSubview(labelTick)
These objects are stacked in the order in which they are added to their superview.
Instead of adding the labelTick subview to the imageView you want to add it to the super view before the image imageView:
var imageTick:UIImage = UIImage(named: "Trash-icon.png")
var imageView:UIImageView = UIImageView(frame: CGRectMake(self.bounds.size.width-50, self.bounds.size.height/2, 30, 30))
imageView.image = imageTick
self.addSubview(labelTick)
self.addSubview(imageView)
Alternatively you can also use insertSubview, to insert them above or below another one in the superview:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/insertSubview:aboveSubview: