问题
As per my requirement, want to access iOS device photos app file url (not file as data) for my GCDWebUploader. I want assets library url for my web server.
NSString* documentsPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
_webServer = [[GCDWebUploader alloc] initWithUploadDirectory: documentsPath];
// this is working and document directory files shown on browser.
_webServer = [[GCDWebUploader alloc] initWithUploadDirectory:assetsUrl]; // this is not working.Nothing shown on browser. //assetsUrl= assets library url for file from photos app
_webServer.delegate = self;
_webServer.allowHiddenItems = YES;
[_webServer start];
my web-server display all the photos app images and videos on pc browser if document directory.this functionality already done using GCDWebUploader. but I can't find asset url behave like file path.
I don't want to copy the photos app files into document-directory and use.but directly access from assets library.
I want assets url work same like document directory filepath. please help me for that.
回答1:
An asset URL looks like this:
assets-library://asset/asset.JPG?id=CD12228F-0E99-4ABD-999D-6A76F54024E7&ext=JPG
This is an internal URL to ALAssetsLibrary
which means nothing outside of this context. You can't expect to pass this URL to GCDWebServer and expect the server to magically do something with it.
Furthermore, GCDWebServer by definition can only serve URLs with the HTTP
scheme, with the hostname matching your iPhone/iPad network name, and with paths for which you have implemented handlers.
For instance if you have implemented a GET
handler for the path /photos/index.html
, then connecting to your iPhone/iPad using your web browser at http://my-device.local/photos/index.html
will call the corresponding handler on GCDWebServer, which then can return some content (like an HTML web page or an image file).
Connecting however to assets-library://asset/asset.JPG
from your web browser doesn't mean anything and will fail.
Connecting to http://my-device.local/asset.JPG?id=CD12228F-0E99-4ABD-999D-6A76F54024E7&ext=JPG
will also fail if you don't have a GET
handler in GCDWebServer for that path.
So in a nutshell, to serve photos from ALAssetsLibrary
using GCDWebServer, you can do it as such:
- Implement a default handler to catch all
GET
requests - Implement a handler for
GET
requests to/index.html
(you must add it to the GCDWebServer instance after the default handler)
In the implementation of the /index.html
handler, you return an HTML web page that lists the URLs of the photo assets from ALAssetsLibrary
, each of them having a relative URL link like <a href="/asset.JPG?id=CD12228F-0E99-4ABD-999D-6A76F54024E7&ext=JPG">My photo link</a>
(the path portion of the asset URL).
In the implementation of the default handler, you retrieve the path of the GCDWebServerRequest
, prepend assets-library://asset
, and that gives you back the original asset URL: assets-library://asset/asset.JPG?id=CD12228F-0E99-4ABD-999D-6A76F54024E7&ext=JPG
. With this URL, you can finally retrieve the asset data, i.e. the JPEG image, and return it using a GCDWebServerDataResponse
(don't forget to set the MIME type to image/jpeg
).
来源:https://stackoverflow.com/questions/31382070/convert-ios-photo-album-path-url-same-like-as-document-directory-file-path