-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

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Khushbu Desai
  • 1,003
  • 1
  • 10
  • 32
  • 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://`) – vadian Jun 19 '18 at 12:36
  • @vadian Thanks man! it works – Khushbu Desai Jun 20 '18 at 04:20

3 Answers3

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
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
Khushbu Desai
  • 1,003
  • 1
  • 10
  • 32
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)
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
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.

Vijay Kharage
  • 401
  • 4
  • 11