I\'m currently developing an iPad app where a user will enter a photo filename in a text field (as part of field notes), then later they will import their photos to the iPad
Extract the URL from the ALAsset, looks like this: assets-library://asset/asset.JPG?id=1000000001&ext=JPG
If you need different file names, make an internal-external paradigm:
#import <Foundation/Foundation.h>
@interface NSURL (NSURL_Asset)
- (NSURL*) toExternalForm;
- (NSURL*) fromExternalForm;
- (NSString*) toExternalFilename;
@end
#import "NSURL+Asset.h"
#import "URLParser.h" // from http://iphone.demay-fr.net/2010/04/parsing-url-parameters-in-a-nsstring/
static NSString *const EXTERNAL_TOKEN = @"/assetExternalForm/";
@implementation NSURL (NSURL_Asset)
// assets-library://asset/asset.JPG/assetExternalForm/1000000001.JPG -> assets-library://asset/asset.JPG?id=1000000001&ext=JPG
- (NSURL*) fromExternalForm {
if([self.scheme isEqualToString:@"assets-library"]) {
NSRange slash = [self.absoluteString rangeOfString:EXTERNAL_TOKEN options:NSBackwardsSearch];
if(slash.location != NSNotFound) {
NSRange dot = [self.absoluteString rangeOfString:@"." options:NSBackwardsSearch];
if(dot.location != NSNotFound) {
NSString* extention = [self.absoluteString substringFromIndex:(dot.location + dot.length)];
NSString* identifier = [self.absoluteString substringWithRange:NSMakeRange(slash.location + slash.length, dot.location - (slash.location + slash.length))];
return [NSURL URLWithString:[NSString stringWithFormat:@"%@?id=%@&ext=%@", [self.absoluteString substringToIndex:slash.location], identifier, extention]];
}
}
}
return self;
}
// assets-library://asset/asset.JPG?id=1000000001&ext=JPG -> assets-library://asset/asset.JPG/assetExternalForm/1000000001.JPG
- (NSURL*) toExternalForm {
if([self.scheme isEqualToString:@"assets-library"]) {
NSRange range = [self.absoluteString rangeOfString:@"?"];
if(range.location != NSNotFound) {
URLParser *parser = [[[URLParser alloc] initWithURLString:self.absoluteString] autorelease];
NSString* extention = [parser valueForVariable:@"ext"];
NSString* identifier = [parser valueForVariable:@"id"];
if(extention != NULL && identifier != NULL) {
return [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@.%@", [self.absoluteString substringToIndex:range.location], EXTERNAL_TOKEN, identifier, extention]];
}
}
}
return self;
}
// assets-library://asset/asset.JPG?id=1000000001&ext=JPG -> 1000000001.JPG
- (NSString*) toExternalFilename {
if([self.scheme isEqualToString:@"assets-library"]) {
NSRange range = [self.absoluteString rangeOfString:@"?"];
if(range.location != NSNotFound) {
URLParser *parser = [[[URLParser alloc] initWithURLString:self.absoluteString] autorelease];
NSString* extention = [parser valueForVariable:@"ext"];
NSString* identifier = [parser valueForVariable:@"id"];
if(extention != NULL && identifier != NULL) {
return [NSString stringWithFormat:@"%@.%@", identifier, extention];
}
}
}
return NULL;
}
@end
Note that you do not need a filename to read the content of an ALAsset. Use the ALAsset.defaultRepresentation.getBytes method for that.
I was able to get an image's original filename like this:
NSString* originalFileName = [[asset defaultRepresentation] filename];