So I am coding in Xcode 5 and I am working on a project that allows people to save an image that is displayed on the image view. I typed in this code...
[starImageView initWithImage:[UIImage imageNamed:#"star.jpg"]];
And then this error/warning came up...
"Expression result unused"
If you have ANY other questions about my project, let me know!
ALL answers are GREATLY appreciated!!!
Thanks!
Eric
According to Apple documentation, initWithImage is an instance method, which returns an UIImaveView object instance.
It does two things:
1: adjusts the reciever's frame (in your case starImageView) to match the size of the specified image
2: Returns another UIImageView initialized with the specified image.
Why you are getting the warning:
You are not assigning the object returned by the method call. You should have something like this to get rid of this warning:
id newImageView = [starImageView initWithImage:[UIImage imageNamed:#"star.jpg"]];
(Then you will get a warning unused variable for newImageView, as you are assigning value but not using it anywhere)
If starImageView has already been initialised and you are trying to set an image to imageview then you should use following statement instead.
[starImageView setImage:[UIImage imageNamed:#"star.jpg"]];
Related
I am using swift and xcode for a quiz app (first app) and I want to insert and image that corresponds with the question. This is what I have so far:
Array for images:
var pictures: [UIImage] = [(#imageLiteral(resourceName: "americanmap")), (#imageLiteral(resourceName: "map")), (#imageLiteral(resourceName: "Thumbs_Up_Hand_Sign_Emoji_large"))]
Function that displays the image (it is inside the new question function):
images = pictures[currentQuestion]
On this part I get an error message: "Cannot assign to immutable expression of type[UIImageView]"
By the way, images is an outlet of the image on the storyboard
Thank you!!
I fixed it! I changed the code from:
images = pictures[currentQuestion]
to:
images.image = pictures[currentQuestion]
which I think changed its type. Thank you guys so much for your help!!
I have a Today Extension with a button and UIImage. The button and image scale and center themselves correctly except the button's text is not scaled. I am writing this extension in Swift. I have tried these so far:
myButton.titleLabel.adjustsFontSizeToFitWidth = true
myButton.adjustsFontSizeToFitWidth = true
Both return errors.
Without seeing more code or the specific errors, the only thing I can see is that titleLabel is an optional. As written, the compiler would show the error 'UILabel? does not have a member named 'adjustsFontSizeToFitWidth'. Writing the line as myButton.titleLabel?.adjustsFontSizeToFitWidth = true should fix the error.
Also, UIButton doesn't have a member named 'adjustFontSizeToFitWidth' so the second line would throw the same error as above.
Try fixing that error as indicated above and see if your AutoLayout issue works.
I'm getting stuck on some code with the dreaded "Can't unwrap Optional.None" error in my code.
I'm following the Shutterbug code from the iTunes U Stanford university course.
This is the code given in Objective-C for one of the classes.
http://pastebin.com/LG2k3BBW
and what I've come up with in Swift;
http://pastebin.com/pGtSzu6z
After tracing the errors these lines in particular seem to be giving me the problems
self.scrollView.zoomScale = 1.0
and
self.image = nil
Any advice on what's going wrong here?
I had originally put all the setters in the ViewDidLoad function and was receiving the same error.
This line is called when you are preparing for segue:
ivc.imageURL = flickerFetcher.URLforPhoto(photo, format: FlickrFetcher.FlickrPhotoFormat.Large)
Which calls the setter on imageURL:
set {
self.startDownloadingImage()
}
startDownloadingImage() calls the setter on image which is where you get all of your errors:
set{
self.imageView.image = image
self.scrollView.zoomScale = 1.0
self.spinner.stopAnimating()
self.imageView.frame = CGRectMake(0, 0, image!.size.width, image!.size.height)
}
Since all of this is happening in prepareForSegue, the view controller hasn't been loaded at this point and all of the outlets are nil hence the can't unwrap Optional.none.
You need to restructure your program's logic so that it isn't trying to do stuff with outlets before they are loaded.
Check your outlets are connected. Outlets are defined as implicitly unwrapped Optionals, so you'd expect to see that error if you referred to a property that wasn't set (e.g. If the outlet was not connected)
I've downloaded your project and there was a problem with the outlets - the scrollview and spinner were kind of grayed out in Xcode. Here's how the scrollview and spinner showed up for me:
This means that the items are not installed for the currently selected size class. If you go to the attributes inspector for that view, make sure the Installed option is checked for the size classes you care about:
In your project, the top box (representing any size class) was not checked.
However, there are many more problems within the code, too numerous to go into in full detail in this answer. You have several issues which are causing problems, including (I did give up after a while):
Infinite loops in your property accessors - for example, the get closure for imageURL
Implementing set or willSet closures when you actually want didSet - for example, the willSet on the scrollView would be better as a didSet, or you should be using newValue instead of scrollView, because at the point of willSet, scrollView is still nil.
Setting a value of nil to your image property, then accessing it in the setter block for that property
Something odd going on in your downloading logic (at this point, I decided to call it a day, sorry)
In my app, i am having the image name in a variable, and I pass the value to the imageView as below
productsView.imageView.image=[UIImage imageNamed:[NSString stringWithFormat:#"%#",imageString]];
The image is not getting displayed,
I have printed my imagestring,
NSLog("%#",imageString);// It gets printed in log as myImage.png
But if I give the value directly, it works with out any mistake
productsView.imageView.image=[UIImage imageNamed:[NSString stringWithFormat:#"myimage.png"]];
Can anyone help me please
I think small mistake. From Your comment, image name should be
imageString = #"myimage.png", not #"myImage.png".
I just tried to set a new image (UIImage object) for my UIButton using myBtn.imageview.image = newimg; then I found it didn't work.
After that I get the correct solution that using setImage:forState: , the documentation about UIButton says "imageView is the button’s image view. (read-only)"
How should I get it? What's the difference?
Thanks!
As the documentation says("imageView is the button’s image view. (read-only)"), it is a read only property and you cant just change the imageView. You have to use the recommended method setImage:forState:. Basically UIButton might be having a different implementation than what you are expecting and myBtn.imageview.image may not actually set the desired image to the button. setImage:forState: might be having an implementation which could be completely different from just setting myBtn.imageview.image = newimg;. By directly setting like in your question, you might get some unexpected results since the actual implementation is not executed.