-2

I have one JSON which contain path of images from one local folder of Project as Followed

enter image description here

The issue is i want to get image from that path, How do i achieve it?

I had try to convert String to URL and set URL as Image using Kingfisher Library

let url = URL(string: "/VWiOSProjects/CollageMakerDemo/Development/CollageMaker/CollageMaker/Goodies.xcassets/Goodies-1.imageset/Goodies-1.png")!

cell.imgTool.kf.setImage(with: url)

But it don't work I had Tried this one also

let url = URL(string: "/VWiOSProjects/CollageMakerDemo/Development/CollageMaker/CollageMaker/Goodies.xcassets/Goodies-1.imageset/Goodies-1.png")!

let imageData:NSData = NSData(contentsOf: url)!
let image = UIImage(data: imageData as Data)

cell.imgTool.image = image

NOTE: I can't upload this JSON file on Server, I need to use it Locally

2
  • 2
    Basically the API URL(string is wrong. For paths in the local file system you have to use URL(fileURLWithPath, URL(string is only for URL string representations which include the scheme (file:// or http://) Commented Jun 19, 2018 at 12:36
  • @vadian Thanks man! it works Commented Jun 20, 2018 at 4:20

3 Answers 3

10

I have solved my issue using fileURLWithPath

let url = URL.init(fileURLWithPath: "/VWiOSProjects/CollageMakerDemo/Development/CollageMaker/CollageMaker/Goodies.xcassets/Goodies-1.imageset/Goodies-1.png")

let imageData:NSData = NSData(contentsOf: url)!

let image = UIImage(data: imageData as Data)

cell.imgTool.image = image
Sign up to request clarification or add additional context in comments.

1 Comment

Note that in Swift you don't need to include the actual .init. The preferred way is URL(fileURLWithPath:
3

If the images are in Assets(*.xcassets) folder, the you can access it by init(named:) method.

cell.imgTool.image  = UIImage(named: "img1")

Actually you no need to store the entire path. You could store image names in array or something.


In my point of view, the best way is to add All Images to *.xcassets folder.(Because you have preloaded 5-10 images)

In case you needs to display it in collection view or TableView,

let imageName = "Goodies-\(indexPath.row)"
cell.imgTool.image  = UIImage(named: imageName)

4 Comments

I need to use it from JSON only
sorry, i didn't get it. Where you stored those images?
If i add Image in another folder of project then i can access it?
@LalKrishna, Thanks for you efforts, Check my post i have done what i exactly want
0

If images are in assets folder itself then go for @Lal Krishna's method. And if they are on server then you should add http:// or https:// and the URL of server followed by JSON's URL. If still you are not getting it then let me know.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.