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".
Related
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
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!!
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"]];
I have a UIImageView in a view of a StoryBoard, IBOutlet connections are made properly (have double and triple checked this), the images to display are added to the proper target, etc. (Have been debugging this for a day now) the UIImageView display images if I set them in Interface Builder.
But if I set the UIImageView.image property to a valid UIImage (I can see even the preview when debugging) loaded with any image it always shows the first image, not the new one, or if I left it blank in IB it keeps nil value.
This only happens in this view if I try the same thing in another view of the StoryBoard and the UIImageView properly shows the content of the UIImage.
Any clues will we greatly appreciated.
Edited:
Thanks for the replies, has tested also setting it from a button, the code is this code:
The property declaration is:
#property (strong, nonatomic) IBOutlet UIImageView *theRecomendationImage;
Have Tested with weak and strong, just to be sure.
The IBAction implementation (tested it gets called)
- (IBAction)changeImage {
UIImage *theImageToTint = [UIImage imageNamed:#"Sunny"];
// theImageToTint = [theImageToTint imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
self.theRecomendationImage.tintColor = [UIColor yellowColor];
self.theRecomendationImage.image = theImageToTint;
}
When debugging if you check the value of theImageToTint it is a valid image (not nil) and you can even quicklook the image. But when I assign it to the UIImageView the value is always nil. No error, just keep nil.
Thanks for your interest, and sorry for the incomplete question.
Edited:
If you execute
po self.theRecomendationImage.image
in the debug console before and after executing the assignment: self.theRecomendationImage.image = theImageToTint; the value is nil.
Edited:
If you add the UIImageView by code, it works and get updated when you assign a new image to it, this takes me to point to some Storyboard issue.
Is there any tool to verify a storyboard?
Just make sure the image format is .png instead of .jpg or any other.
I have declared an UIImageView as an IBOutlet and I am setting the image of the imageView as follows in viewDidLoad method
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString: [productProperties objectForKey:#"image"]]]];
But this image doesn't show up at all.
productProperties is a NSDictionary which stores the url for the key "image"
I even tried storing the image locally and then setting it up like below
imageView.image = [UIImage imageNamed:#"icons_browse.png"];
But that also doesn't work.
Try removing the line of code and setting the "default" image in IB.
I know this is not a fix - but I am curious to see of there is some other reason why it is not visible - like it's set as hidden, has 0% alpha, is obscured by something else, etc.
The code - especially the second thing you tried (assuming the file is actually in your bundle and named properly) is very boilerplate and should work..
The other thing to do is to check the value being returned by [UIImage imageNamed] - and make sure it's not nil - meaning there is actually some problem loading the file. (Wrong file name, file not in bundle, etc.)
Sorry guys, it is my mistake, I am providing the wrong value for the NSURL.
got it fixed