well am trying this :
if let storedCoverImagePath = UC.coverImagePath{
let storedCoverImage = UIImage(contentsOfFile: storedCoverImagePath as String)
self.coverImage.image = storedCoverImage
self.backgroundImageView.image = storedCoverImage
}
and the value of storedCoverImagePath is :
/Users/remy/Library/Developer/CoreSimulator/Devices/D29D4F6F-E146-419D-B4B8-B1914F56569F/data/Containers/Data/Application/637675BB-EFA1-A8RT-8B73-A3628/DocumentsCoverImage.jpg
its straight forward that am trying to get an image from this storedCoverImagePath path. i double checked the path, image is there still am not able to set the image using this path, is there is something that am missing if yes than tell me please
You have to specify path relative to your apps bundle or directory. If you hardcode the path like you did:
/Users/remy/Library/Developer/CoreSimulator/Devices/D29D4F6F-E146-419D-B4B8-B1914F56569F/data/Containers/Data/Application/637675BB-EFA1-A8RT-8B73-A3628/DocumentsCoverImage.jpg
it gets ignored because of the sandboxing. iOS does not allow the app to reference directories external to the sandbox.
Possible solutions
One way to get it working is to add the image to the project. Then you can get it using:
let image : UIImage = UIImage(named:"ImageName")
Another way, if the image is saved in documents directory of your app, is to use:
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let fileURL = documentsURL.URLByAppendingPathComponent(yourImageName)
let image = UIImage(contentsOfFile: fileURL.path!)
Related
I'm trying to save images using their file paths but every time that I kill the Xcode simulator and restart it, the location of the images changes and my imageView is blank. I know for a fact that the location changes because I print out the file path when I try to pick an image from the photo library and it's different each time.
I access the URL of the UIImage
let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
let url = info[UIImagePickerController.InfoKey.imageURL] as! URL
After which I save the URL as a string to my realm database. I've tried converting it to string via all of these
let imageFilePath = url.relativeString
let imageFilePath = url.absoluteString
let imageFilePath = url.relativePath
but none of them worked. I'm a newbie to Swift so let me know if I've missed any key details needed.
I have one JSON which contain path of images from one local folder of Project as Followed
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
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
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)
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.
I'm making an app with the folder like image below:
As you can see in this picture, Stickers folder have 2 sub folder "1" and "2". In side them is bunch of icon and I wanna load it in a collection view with each sub folder is a package of different sticker. For more details, please see this picture:
So, how can I get it in my project folder? I've read about access document directory but seem like it not solve my problem.
Please help me. Thanks in advance.
Look at Filemanagers API. It offers all you need.
Example to get content at path:
let pathToDir1 = Bundle.main.resourcePath! + "/Stickers/1";
let fileManager = FileManager.default
let contentOfDir1 = try! fileManager.contentsOfDirectory(atPath: pathToDir1)
Example to iterate over:
let docsPath = Bundle.main.resourcePath!
let enumerator = fileManager.enumerator(atPath:docsPath)
let images = [UIImage]()
while let path = enumerator?.nextObject() as! String {
let contentOfCurDir = try! fileManager.contentsOfDirectory(atPath: path)
// do whatever you need.
}
For details see Apples documentation and sample code.
If you're looking to access an image within your project folder you simply need to use the image name. You don't need to define the whole path.
Objective-C
UIImage *img = [UIImage imageWithName:#"IMAGE_NAME"];
swift
var img = UIImage(named:"IMAGE_NAME")
Below are the two functions which I use in the picker view controller to save the file in the App documents directory. And save the url in the local DB & display the image using UIImage(contentsOfFile: ImagePathString)
Everything works well as until this point however as soon as i re-run the simulator images does not show up. I believe its because the app directory keeps changing every time its run on the simulator. My question if its the same when deployed on the device?
func getDocumentsDirectory() -> NSString {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory
}
//Below function is used in imagePickerController
if let data = UIImagePNGRepresentation(modifiedImage!) {
let filename = getDocumentsDirectory().stringByAppendingPathComponent("\(NSDate()).png")
data.writeToFile(filename, atomically: true)
pathImage = filename
print(pathImage)
}
Yes url to App documents folder may change when the app is deployed on the device.
Refer below link
Technical Note TN2406
.
I have bunch of tile images inside the folder called "Tiles"
I need to extract one image at a time from that folder, and set it to UIView object.
I am stuck on how to get the image from inside that folder.
Can anyone help do this in Swift?
If you have the image name,you can just access the image in this folder like this
let image = UIImage(named: "image.png")
Or this
let imageURL = NSBundle.mainBundle().URLForResource("image", withExtension: "png")
let image = UIImage(contentsOfFile: imageURL!.path!)
If you want get all images,you create a real folder inside the project
let imageArray = NSBundle.mainBundle().URLsForResourcesWithExtension("png", subdirectory: "Titles") as! [NSURL]
This will return an urlArray of png inside Titles folder.You can use contentsOfFile as show before to access image