Swift: How to Get Image Name from Assets

Swift: How to get image name from assets

You cannot do what you are describing while storing the images in the Assets Catalog. Fetching a resource from an Assets Catalog relies upon your knowing its actual name.

Instead, store these folders directly at the top level of your app bundle. Now you have an actual folder that you can get a reference to, and can ask the FileManager for the names of the images in the folder.

Sample Image

Example (Swift 2.2, sorry):

let f = NSBundle.mainBundle().URLForResource("carPix", withExtension: nil)!
let fm = NSFileManager()
let arr = try! fm.contentsOfDirectoryAtURL(f, includingPropertiesForKeys: nil, options: [])
print(arr) // the URLs of the two jpg files

How to load specific image from assets with Swift

You cannot load images directly with @2x or @3x, system selects appropriate image automatically, just specify the name using UIImage:

UIImage(named: "green-square-Retina")

How to find specific images which start with a string in Swift Assets.xcassets

Assets folder cannot be accessed this way. One way to do that is creating a folder with all the images on your computer, and drag into the Xcode project. Dont forget to select the "Create folder references for any added folders" option. With that referenced folder, you can access all images:

guard let _resourcePath = Bundle.main.resourcePath else{
return
}
do{
if let url = NSURL(string: _resourcePath)?.appendingPathComponent("YOUR FOLDER NAME"){
let resourcesContent = try FileManager().contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)

for imageUrl in resourcesContent {
let imageName = imageUrl.lastPathComponent
print(imageName)
//CHECK IMAGE NAME STRING
}
}
}catch let error{
print(error.localizedDescription)
}

Can you get image file name from assetURL in IOS?

I figured it out if anyone else is interested in doing the same thing. Credits to the link I posted in question I just translated it to Swift.

    let assetLib = ALAssetsLibrary()
//Save image get access to the url in completion handler
assetLib.writeImage(toSavedPhotosAlbum: imageToSave.cgImage, orientation: ALAssetOrientation(rawValue: imageToSave.imageOrientation.rawValue)!, completionBlock: {(url,error) -> Void in
let phAsset = PHAsset.fetchAssets(withALAssetURLs: [url!], options: nil)
let lastPhAsset = phAsset.lastObject
let asset = assetLib.asset(for: url, resultBlock: {(asset) -> Void in
//print("Asset Success!")
let imageRequestOptions = PHImageRequestOptions()
imageRequestOptions.isSynchronous = true
PHImageManager.default().requestImageData(for: lastPhAsset!, options: imageRequestOptions, resultHandler: {
(data,string,orientation,diction) -> Void in

let filename = diction![AnyHashable("PHImageFileURLKey")]!
// You should have the correct filename here.
//print("Filename = \(filename)")
})

}, failureBlock: {(error) -> Void in
print("Failed to retrieve a asset from url.")
return
})

})

How to get image from assets by folder ios

you can get image depending on folder by "Providing Namespace" to folder.

  1. Select folder in .xcassets
  2. Check "Providing Namespace" in last tab (see attached image)
  3. Access image using var image : UIImage = UIImage(named:"New Folder/bgimg")

Sample Image



Related Topics



Leave a reply



Submit