How do I add an image to my quiz - ios

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!!

Related

I'm facing with one little issue can you help me guys? any advice appreciated

This should be portray asset image when "ask" button pressed. i wrote all things that i knew but it still show like that
In the image you posted, you defined:
#IBOutlet var ImageView: [UIImageView]!
but then you try to reference it with a lower-case i:
imageView = ballArray...
As a side note, even correcting to an upper-case i you'll get an error, because you've defined ImageView as an array of image views, but you're code trying to assign one image view to it (from your ballArray).

How to test if a UIImageView contains an image with a specific name using XCUITest?

I have a collectionView that has a bunch of cells that contain a single UIImageView in each cell. I want to test wether or not in each cell, the imageView's image matches the correct image name.
In the production code, I've added an accessibility identifier to the UIImageView example: "My Image View". I loop through an array of strings containing image names and set the cell's image in accordance to the index, example: ["image0.png", "image1.png", "image2.png"] so cells at index 0-2 would have those images respectively.
In my XCUITest file I'm trying something like this:
XCTAssert(cells.images["My Image View"].exists, "message here")
But this doesn't check if the cell's imageView has the right image. Can anyone suggest a better solution?
Some references I visited beforehand:
How to test UIImageView elements in iOS XCTest?
XCUIElement - Obtain Image value
I apologize if this question has been asked before, I couldn't find anything.
Edit:
I found a solution.
let image = cells.element(matching: , identifier: )
I didn't know that the identifier parameter actually uses the image name so If I pass the image name, I can assert the existence of the image.
let image = cells.element(matching: .image, identifier: "myImage.png")
this works. So when I loop through an array of strings containing the image name, I can also check if the cell at the index corresponding to the image index is correct.
I also forgot to mention that the images aren't being stored in assets, but are being fetched via json.
cesarmarch's answer below was the closest so I marked that as correct.
XCTest UI tests are designed to be written as functional tests, rather than checking that the display is correct. Since the image doesn't exhibit a behaviour for the UI test to observe, a UI test isn't the best tool for the job.
In this case, you should write unit tests to assert that the correct image is assigned to your image views, as unit tests will have access to the right level of information to allow you to inspect the data you pass to your view presentation layer and assert that the assigned image is the one you expect.
I just use something like
XCTAssert(cells.images["Image_Name_In_Resource_Directory"].exists, "message here")
to check if the current image is the good to use and it works fine.
All the images in the project are exported into the app bundle while building. Have a folder under your UI Testing group to contain all the expected images to be verified and a dummy image file.
var url = Bundle(for: AnyClass.self).url(forResource: "<ExpectedImageName>", withExtension: "<imageExtension png/jpg/etc>")!
let expectedImage = NSImage(contentsOf: url)!.tiffRepresentation!
url = Bundle(for: AnyClass.self).url(forResource: "<NameOfDummyImageFile>", withExtension: "png")!
try? cells.images["My Image View"].screenshot().pngRepresentation.write(to: url)
let actualImage = NSImage(contentsOf: url)!.tiffRepresentation!
XCTAssert(actualImage.elementsEqual(expectedImage), "Images are not same.")
The above code works for me.. Hope this will work for you too

Image view found nil when unwrapping

I have an image that I download from Parse and load into an image view. When I do so I get the following error (with included print of image to show existence):
<UIImage: 0x13707c950>, {2045, 2588}
fatal error: unexpectedly found nil while unwrapping an Optional value
It is definitely linked up. I have tried cleaning, restarting Xcode, and deleting the view and image and remaking it. I have a companion project that is similar to this, and uses the same code (in a separate method I convert the PFFile into the image). I also know this method works because at another part in the project I load the image in this exact way.
if let image = objectsKeyValues[indexNumber]["image"] {
print(image)
sALargeNoteImage.imageView.image = image as? UIImage
sALargeNoteImage.imageView.contentMode = UIViewContentMode.ScaleAspectFit
}
I can't for the life of me figure out what's happening. I have done plenty of reworking in the storyboard, even at one point refactoring. This could suggest that it is related (as I have read on SO) to Xcode getting "confused". Not really sure what I should try next, short of remaking the storyboard its on (that would be a lot of work). I suppose I could remake it and copy paste the code..
Anybody have any ideas? I don't use a simulator, instead I load it onto an iPad. I have tried deleting and reinstalling the app.
The part that gets highlighted from the error is:
sALargeNoteImage.imageView.image = image as? UIImage
update:
var sALargeNoteImage = SALargeNoteImage()
class SALargeNoteImage: UIViewController {
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
sALargeNoteImage = self
}
At one point I was testing out if it would clear the image when I set:
lSLargeNoteImage.imageView = nil
vs
lSLargeNoteImage.imageView.image = nil
I had left it like that by accident, and the function that does that runs before this.
Since you are creating sALargeNoteImage using a simple init, it will not be connected to any of the outlets in the storyboard.
You need to use something like
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let sALargeNoteImage=storyboard.instantiateViewControllerWithIdentifier("largeNoteViewController") as! SALargeNoteImage
in order to instantiate the view controller and link all of the IBOutlets
I would highly recommend that you avoid the use of var someVariable=SomeType() if all you are trying to do is suppress the error from the compiler that properties must be initialised and you intend to initialise the property properly later. It is much better to use var someVariable:SomeType! as this will throw an exception if you forget to assign a value to the property rather than resulting in obscure bugs where a default value is used.

Xcode 5 Error: Expression result unused

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"]];

error while passing the value to the imageView dynamically in iOS?

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".

Resources