4

The iPhone UIImagePickerControllerReferenceURL returns an URL as:

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

What's the best (preferably simple) way to retrive 1000000001 and PNG as NSStrings from the above URL example?

ohho
  • 49,193
  • 73
  • 246
  • 377

2 Answers2

4

Well, you can easily turn it into an NSURL by using +[NSURL URLWithString:]. From there you could grab the -query string and parse it out, something like this:

NSString *query = ...;
NSArray *queryPairs = [query componentsSeparatedByString:@"&"];
NSMutableDictionary *pairs = [NSMutableDictionary dictionary];
for (NSString *queryPair in queryPairs) {
  NSArray *bits = [queryPair componentsSeparatedByString:@"="];
  if ([bits count] != 2) { continue; }

  NSString *key = [[bits objectAtIndex:0] stringByRemovingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  NSString *value = [[bits objectAtIndex:1] stringByRemovingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

  [pairs setObject:value forKey:key];
}

NSLog(@"%@", pairs);

Warning, typed in a browser, so some of my spellings may be wrong.

Dave DeLong
  • 241,045
  • 58
  • 447
  • 497
0

For IOS >= 4.0 you can use native regular expressions with NSRegularExpression class. Examples you can find here

Valerii Pavlov
  • 1,946
  • 1
  • 19
  • 30