unresolved identifier for an image in assets when trying to use it programmatically - swift

I'm trying to learn how to use autolayout programmatically in Swift using this tutorial. I added the PNG to the Assets folder, but when I try to add an UIImageView in the ViewController
let imageView = UIImageView(image: bear)
I get the error "use of unresolved identifier 'bear'".
However, if I go to the Main.storyboard, I can add the image fine. Any tips on why this image isn't recognized in the ViewController? (I tried this with multiple PNGs in case it was a bad image, but same result).

bear needs to be a type UIImage, try something like this
func addImage() {
let imageView = UIImageView()
let bear = UIImage(named: "bear") // whatever the name of that image file is, within your assets
imageView.image = bear // setting your bear image here
}

Related

Why assigning self.imageView to a new constant?

I came across the below Swift code snipped in Firebase documentation here:
// Reference to an image file in Firebase Storage
let reference = storageRef.child("images/stars.jpg")
// UIImageView in your ViewController
let imageView: UIImageView = self.imageView
// Placeholder image
let placeholderImage = UIImage(named: "placeholder.jpg")
// Load the image using SDWebImage
imageView.sd_setImage(with: reference, placeholderImage: placeholderImage)
In this particular example, why did they need to assign self.imageView to a new constant and then use that new imageView instead? Why not using the main self.imageView?
Thank you!
The only reason I can imagine is that they wanted to make it clear that an image view exists. There is absolutely no reason to reassign the image view, especially considering that they have the same exact name.

Playground, unable to set same UIImageView to more than one UIView

I've got this code
override public func viewDidLoad() {
let imageBlankCard = UIImage(named: "BlankCard.png")
let imageViewBlankCard = UIImageView(image: imageBlankCard)
rockCard.addSubview(imageViewBlankCard)
scissorCard.addSubview(imageViewBlankCard)
paperCard.addSubview(imageViewBlankCard)
self.view.addSubview(rockCard)
self.view.addSubview(scissorCard)
self.view.addSubview(paperCard)
}
But when I run the playground, only the latest UIView (paperCard) is displayed properly, the other cards are not shown at all as you can see in the image here: there should be two cards like the last one, in place of the arrows.
Can someone help me? Thanks everybody!
According to the documentation,
Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.
This explains the behaviour that you're getting.
One solution to this is to create three separate image views and add each of them as a subview.
Judging by the variable names, I think you are creating something similar to a rock paper scissors game. And you have three views that each holds an image view with the image of a blank card. And you want to add the image of a stone, paper and scissors as an overlay to the image view. If that's what you want to do, why not just make rockCard, paperCard and scissorsCard themselves UIImageViews and set the image to be BalnkCard.png?
let imageBlankCard = UIImage(named: "BlankCard.png")
rockCard.image = imageBlankCard
scissorsCard.image = imageBlankCard
paperCard.image = imageBlankCard

Swift - Is there an UIImageViewDelegate?

So I have been out of the iOS coding world for a bit and I am going through Swift at the moment.
In Objective-C I was able to change an image on the fly, but to do so I had to set the image delegate. The other reason I set the delegate was so I could hide the UIImageView
In Swift I seem to have an issue with this.
With the playground I can do this fine:
let responseImageView:UIImageView!
responseImageView.hidden = true
But in the project this does not work.
Can someone help me out here?
In my project I have this code :
#IBOutle var responseImageView: UIImageView!
//Inside a function I have this:
let smile = "smile.png"
imageName = UIImage(named: smile)!
responseImageView = UIImageView(image: imageName)
That code does not work.
And I can't hide the image by doing this:
responseImageView.hidden = true
If you created the actual UIImageView in the Storyboard or Interface Builder (and connected the IBOutlet correctly - it's always a good idea to double check this if something isn't working as expected!) you should not try to instantiate it again in code just to change the image. Instead, change the image by accessing the UIImageView's .image property, like so:
let smile = "smile.png"
let image = UIImage(named: smile)
responseImageView.image = image
Edit:
Just to clarify: by "instantiate" I specifically mean this line from you project code:
responseImageView = UIImageView(image: imageName)
Doing this in your function will break the connection you've set up in the storyboard. If you put a break point after that line or a println("\(responseImageView.superview)") you'll see that your instance of responseImageView is now no longer attached to a superview (explaining why you can change it's image or hide or unhide it all you want - you'll never see any change in the app).

Random image generator

Im trying to display a random image after the push of a button. I have named the 4 images "gameBall1, gameBall2, gameBall3 and gameBall4" and made outlets for them
Here is my attempt:
#IBAction func startButton(sender: UIButton) {
ImageView.image = UIImage(named: "gameBall(arc4random_uniform(3) + 1).png")
}
I get an error saying "Use of unresolved identifier "ImageView""
Lots of problems.
If it says undefined identifier ImageView then it means that you don't have a variable ImageView that is tied to your UIImageView. You need to fix that.
(Open your view controller's scene in the Storyboard. Open the assistant editor and display the view controller's source code. Control-drag from the image view in interface builder into the body of your view controller to define an outlet.)
(You should name variables beginning with lower case letters btw.)
Your code to set the image name is also wrong. You should create a temporary variable with the name of the image in it and display it with a println statement to check it. You are missing a backslash around the (arc4random_uniform(3) + 1) part. It should read
imageView.image = UIImage(named: "gameBall\(arc4random_uniform(3) + 1).png")
First of all check if your IBOutlet is set up correctly to your UIImageView.
Then you can try it like this:
EDIT:
In order to get random number between 1 and 4 you should use arc4random_uniform(4) + 1 :
#IBAction func startButton(sender: UIButton) {
ImageView.image = UIImage(named: "gameBall\(arc4random_uniform(4) + 1).png")
}

Swift display image UIImageview

How can I display image inside UIImageview? Then change images according to pressed button.
I am very new please go easy.
To do this, first make sure you have the image available in the viewcontroller (by creating an association between the image on your storyboard, and in your viewcontroller).
Let's pretend it's called imageView.
Programmatically, you can say:
imageView.image = UIImage(named: ("nameofasset"))
What I actually ended up doing is creating a static function responsible for determining which image to return as such:
class func GetImage(someValueWhichDrivesLogic: Int)->UIImage
{
if (someValueWhichDrivesLogic == 1)
{
assetName = "imageone" //note you don't add .png, you just give it the same name you have in your standard images folder in XCode6
}
else if (someValueWhichDrivesLogic == 2)
{
assetName = "imagetwo" //again, this is the actual name of my image
}
//Return an image constructed from what existed in my images folder based on logic above
return UIImage(named: (assetName))
}
So now that the function above has been created, you could just do something like this:
imageView.image = GetImage(1)
imageView.image = GetImage(2)
This would actually assign the images dynamically to the image. You could just do this, or if you're literally doing something as simple as putting it into a button click you can just take the easier approach I specified above.
Thanks let me know if any questions!