How to display an UIImage in Swift - iphone

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

Related

Trying to add two images in a UI image

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.

Selecting the name of a UIButton swift5

I have a UIButton called picPressed, this UIButton has a background image. I want to to be able to get the name of the buttons background image.
I am trying something like:
let hello = picPressed.backgroundImage.name()
Print(hello)
But get an error saying:
value of type '(UIControl.State) -> UIImage?' has no member 'name'
How can isolate this attribute in swift5?
No, buttons don't remember their names (or the name of the image you install into them.)
I suggest you create an object that manages a button, takes a filename for an image, and remembers that filename for later. (Alternately you could subclass UIButton, but that can get complex.)

Slack Thumbnail url from Slack api for any file type like Image, Pdf, Audio, Video etc

I need to extract thumbnail url for any particular file.
while accessing the https://slack.com/api/files.list
I saw that for image thumbnail key asfiles : thumb_64, thumb_80, 360 type JSON keys
For pdf it is like
& similarly for other file type it has different different json key to find thumbnail.
So for every particular file type should i find thumbnail key manually. Any shortcut to find thumbnail key for all file type.
I need to pass my thumbnail url to some service no matter what kind of file it is.
Thanks!
The documentation for the file object type gives some information on this. But as you have already noticed, it doesn't mention PDF or other file types.
If a thumbnail is available for the file, the URL to a 64x64 pixel
will be returned as the thumb_64 prop.
The thumb_80 prop (when present) contains the URL of an 80x80 thumb.
Unlike the 64px thumb, this size is guaranteed to be 80x80, even when
the source image was smaller (it's padded with transparent pixels).
A variable sized thumb will be returned as thumb_360, with its longest
size no bigger than 360 (although it might be smaller depending on the
source size). Dimensions for this thumb are returned in thumb_360_w
and thumb_360_h.
In the case where the original image was an animated gif with
dimensions greater than 360 pixels, we also created an animated
thumbnail and pass it as thumb_360_gif.
Depending on the original file's size, you may even find a thumb_480,
thumb_720, thumb_960, or thumb_1024 property.
I did try looking through the Slack client's JS code (search for thumb_pdf in client-boot-imports.XXXX.min.js), and found a code block where it seems to be defining possible thumb_* keys. While it is not conclusive, you may look for these fields in the API response and fallback to a default image for each supported format.

Could not load the “” image referenced from a nib in the bundle with identifier

When I put my images in the playingwithcontainerviews folder, they show up on my app. When I put my images in my imagesofcuteanimals folder, they don't. I have targets selected for the folder. This is the error: Could not load the “” image referenced from a nib in the bundle with identifier.
The blue folder is reference type when you load the image access it like this
let img = UIImage(named:"folderName/imageName.jpeg")
OR
if let img = Bundle.main.path(forResource:"imageName",ofType:"jpeg",
inDirectory:"folderName") { }

Swift: Simple - how do I refer a button's name dynamically?

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