How to get a photo's original filename in iOS?

后端 未结 2 876
青春惊慌失措
青春惊慌失措 2020-12-05 15:24

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

相关标签:
2条回答
  • 2020-12-05 16:13

    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.

    0 讨论(0)
  • 2020-12-05 16:15

    I was able to get an image's original filename like this:

    NSString* originalFileName = [[asset defaultRepresentation] filename];
    
    0 讨论(0)
提交回复
热议问题