I've been searching this goddam question for over an hour and it's so simple!
I have 9 images and I want to refer to any one of them in a function. They're all named "img1" "img2" etc. They're also linked to my code with IBOutlets.
Func changeImage(imgNumber: Int){
imageName = "img" + String(imgNumber)
self.imageName.image = UIImage( named: "smiley.jpg") }
Instead of the imageName variable I know I can just refer to the actual object by saying "self.img1.image" but that wouldn't allow me to dynamically alter any image with a function.
Please help!
Put your image in an Asset Catalog.
(You can create one by New file... > iOS Resource > Asset Catalog).
In the asset catalog give your images name like image1, image2, image3 etc.
UIImage(named: "image1") will find images in the Asset Catalog.
The benefit of using Asset Catalog is that you can easily add images supporting the different screen resolutions there as well.
Optionally load them manually, and put them in an array and access by index if there is a logical order to be used.
You should put them in to a collection (in viewDidLoad for example) that preserves the order, ie.
var images = [self.img1, self.img2, self.img3]
and use the index to access the specified button
images[idx].image = UIImage(named: "smiley.jpg")
If you need even more dynamic order, you could use tag for this and traverse the subviews to find the specified view. I wouldn't recommend this though, as it is a pain to debug.
You could use outlet collections but to my understanding they don't preserve the order. This would mean you'd have to find another way to sort the buttons (x-coordinate, y-coordinate or tagging).
Related
Im new to xcode and was trying to add different images in a table row in a storyboard layout.
let image:UIImage = UIImage(named:
”ster.jpg")!
when i use this code, it makes every table the same image
let image:UIImage = UIImage(named:
”ster.jpg", "blue.jpg")!
My goal is for the 2nd row of the table to have a different image, so when using this code it just creates an error saying " Extra argument in call"
I would start by saying that the way you initialize the UIImage object is wrong. It supports only one image at a time. Thus, you get the error of an extra argument in the call.
To add two images in a row, you better use a horizontal stack view and modify some of its properties (fill equally, spacing, etc).
The way I would do the thing: Drag and drop from library 2 UIImage objects. Embed them in a horizontal stack view. In property inspector set the fill property to equal. Make sure to add constraints.
In case you are using UITableView and want to modify the cell, you will have to create a subclass of UITableViewCell and give it an identifier to use when the TableViewDataSourceDelegate object is fetching the cell data based on the identifier.
I created such a structure with CollectionView, there are two items in one line. The whole structure should go on like this, everything is fine until here, but I must add (only 1) slide at the top. So I want to place one more full width image in 1 line. What path should I follow?
An image of my app
If the full-width image that you need to display is just a first item of the data set you're displaying, make the collection view return a custom size for it based on its index path. In order to do it, assign a delegate to your collection view and implement the UICollectionViewDelegateFlowLayout protocol, specifically the collectionView(_:layout:sizeForItemAt:) method. Alternatively, if you're targeting iOS 13+, you can do the same with the UICollectionViewCompositionalLayout, but the implementation will be different, in that case I encourage you to read its documentation.
If the item doesn't really belong to the data set, better create a section header with just that item inside.
I simply would like to display an image which is part of the app.
When executing the following code, my image is nil.
let image = UIImage(named: "back.png");
The "back.png" is stored in my "Images.xcassets" folder of my project.
How to load the image from within the app folders?
(Loading the Image from a path is working for me, but I do not know how to reference an image stored in the project itself)
With the latest Xcode 8.3 its much easier to adding any image, Using Image Literal option.
Just select any image from assets.
let image = //type and select Image Literal option, and select image from as per below reference
You need to use the name of the Image Set, inside Images.xcassets that back.png is stored in, instead of the actual file name.
From the Asset Catalog above (obviously there would be some images in there though), you would use the code:
let image = UIImage(named: "Image")
Have a look at About Asset Catalogs for more information.
Don't use file extension if you place image in Images.xcassets. You need to use the name of the Image Set.
let image = UIImage(named: "back")
Say I have 5 UIImageViews, named
image1
image2
image3
image4
and image5.
And I have 5 buttons, which have their tag values set to the corresponding images that overlay them, so 1 - 5.
If button 1 is pressed, I get the tag value like so
NSNumber buttonNumber = [sender tag];
I then want to perform some methods on the corresponding image (image1). But I want to use the same method for all of the buttons.
So my question is, having retrieved the tag, how can I identify the corresponding image (with the same number after its name). The solution I have so far, which is a bit long winded, is to use a switch:
UIImageView *image;
switch (buttonTag) {
case 1:
image = imageOverButton1;
break;
case 2:
image = imageOverButton2;
break;
default:
break;
// etc
}
But this doesn't seem to be very elegant. If it were a string I could of course use
stringWithFormat:#"imageOverButton%i", buttonTag
So is there an equivalent operation for objects?
I know I could also set the button's background image to the image I want and extract the imageData from the sender but I'd rather do it the above way for various reasons. I suppose I could also add the UIImageViews to an array and extract the relevant object by using the tag.
Thanks! :D
Michael
I think you answered your own question - use an array to hold your UIImageViews and use the tag as an index into that array.
EDIT, folding in my comment: The name of your objects isn't reified - you can't say NSObject foo; and then later be able to reference it with some lookup using #"foo" unless you explicitly make an array mapping names to objects. See another answer I gave to a similar problem.
quick question...
I have a series of buttons, each with a tag. I click the buttons which individually create a uiimageview based on the tag number. So this tag number, say 43 is passed and a new uiimageview is created using 43.png
All this is working nicely and I can remove the created images by clicking on them...
..but... I'm now wondering how I can remove all these created images all at once. So I have say 4 images which were all created as a result of clicking the buttons.
my question is this: can I use a string to identify these "created" images some how? I thought about using a tag for them starting with 99 maybe? so 991, 992, 993 etc. but this doesn't seem like good coding. In the past, and indeed in Flash, I used a tag of item1, item2... then in the code, I simply loop through ALL tags on the screen starting with "item" and remove them.
any ideas on the best way to tackle this??
Thanks
You could simply store a reference to all the created images as elements in an array kept as an attribute of the viewController.
Alternatively, this is the sort of problem that can be handled with a subclass. You can just create a subclass of UIImage with some sort of identifier attribute and use that to remove them.
Seems like you could just loop through the subviews array, look at the tag property of each one, convert each to a string, and use NSString startsWith: to remove those that match your pattern.
But I think it would be easier to just keep your own list of created images, and delete them when desired.