How to detect where the on demand resources are located after being downloaded?

吃可爱长大的小学妹 提交于 2019-12-29 09:17:12

问题


I implement the app thinning in my project by getting the reference from below two answers:-

  1. https://stackoverflow.com/a/33145955/988169
  2. https://stackoverflow.com/a/31688592/988169

How to detect where the on demand resources are located after being downloaded?


回答1:


Unless you specified a bundle when you initialized the resource request, the resources are placed in the main bundle by default. So for example, you could access them this way, if appropriate to your type of resource:

NSString *path= [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"mp4"  ];



回答2:


Finally I've found, how to get a path of on demand resources location:

func preloadResourcesWithTag(tagArray: Array<Any>, resourceName: String ){

    let tags = NSSet(array: tagArray)
    let resourceRequest:NSBundleResourceRequest = NSBundleResourceRequest(tags: tags as! Set)
    resourceRequest.beginAccessingResources { (error) in
        OperationQueue.main.addOperation{
            guard error == nil else {
                print(error!);
                return
            }

                print("Preloading On-Demand Resources ")

            let _path1 = resourceRequest.bundle.path(forResource: "img2", ofType: "jpg")
            let img = UIImage(contentsOfFile: _path1!)}

So, the real location of on demand resources is here:

/Users/admin/Library/Developer/CoreSimulator/Devices/250101ED-DFC5-424C-8EE1-FC5AD69C3067/data/Library/OnDemandResources/AssetPacks/com.ae.OfflineMapSwiftV1/1629348341761109610/com.##.OfflineMapSwiftV1.asset-pack-000000G31NNOJ.assetpack/img2.jpg




回答3:


To expand upon saswanb's answer, the pathForResource you use has to match the folder or filename you added to the ODR tag. If you add a reference to a folder to an ODR, you can search for that folder name, but not the contents of the folder.

NSString *path= [[NSBundle mainBundle] pathForResource:@"ODRFolder1" ofType:nil  ];

But if you add the contents of the folder to the ODR tag, then you can search for each file individually.



来源:https://stackoverflow.com/questions/33824601/how-to-detect-where-the-on-demand-resources-are-located-after-being-downloaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!