开发者

Retrieving a filename for an ALAsset

How can the filename be extracted from an ALAsset?

Is there a way to get this via t开发者_运维技巧he url or some other way?


From iOS 5.0 you can get the file from ALAssetRepresentation Class.

ALAssetRepresentation *rep = [anAssetItem defaultRepresentation];
NSString *fileName = [rep filename];


Update: As yeonsh notes below, from iOS 5.0 there is a better way. This answer is relevant for iOS < 5.0.

You can extract an URL from the ALAsset, but all the filenames are the same, on the form

assets-library://asset/asset.JPG?id=1000000001&ext=JPG

If you for some reason need different file names, try making 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜