-2

This is the URL

assets-library://asset/asset.JPG?id=CF2AF034-9CF7-4472-9185-5EEFA1614A07&ext=JPG

I want to get:

CF2AF034-9CF7-4472-9185-5EEFA1614A07

How would I do that? Is there a method in NSURL that can accomplish this?

This is what I did:

-(NSString *) fgetTokenWithPre:(NSString *) pre andPost:(NSString*) post startSearch:(NSUInteger) start
{
NSRange rangeToSearch;
rangeToSearch.location = start;
rangeToSearch.length =self.length-rangeToSearch.location;

NSRange preRange = [self rangeOfString:self options:NSCaseInsensitiveSearch range:rangeToSearch];

Result

(lldb) po self
$6 = 0x1e54f130 id=00000000-0000-0000-0000-0000000005E9&ext=JPG
(lldb) po pre
$7 = 0x0021d8a0 id=
(lldb) p preRange
(NSRange) $8 = location=0, length=47

But that doesn't make sense. It's obvious that self is 47 length and I am looking for pre that's only 3 length. So how come preRange is [0,47]?

user4951
  • 32,206
  • 53
  • 172
  • 282
  • http://stackoverflow.com/search?q=%5Bobjc%5D+extract+url: [Extract part of url](http://stackoverflow.com/q/3308681), [Parse nsurl query property](http://stackoverflow.com/q/3997976), [How to extract scheme and host of a url once?](http://stackoverflow.com/q/8890475) – jscs Feb 25 '13 at 04:43

2 Answers2

0

The below code block will probably do what you're after

__block NSString *queryID;
[[[url query] componentsSeparatedByString:@"&"] enumerateObjectsUsingBlock:^(NSString *queryString, NSUInteger idx, BOOL *stop) {
  NSArray *query = [queryString componentsSeparatedByString:@"="];
  if ([query[0] isEqualToString:@"id"]) {
    queryID = query[1];
    *stop = YES;
  }
}];

NSLog(@"ID value = %@", queryID);

However, it's not very safe. It makes assumptions about well formed URL's and that there will be (at least) 2 values in the query array.

rickerbh
  • 9,731
  • 1
  • 31
  • 35
0
NSUrl *asseturl = [NSURL URLWithString:@"assets-library://asset/asset.JPG?id=CF2AF034-9CF7-4472-9185-5EEFA1614A07&ext=JPG"];


ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
    ALAssetRepresentation *rep = [myasset defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];
    if (iref) {
        largeimage = [UIImage imageWithCGImage:iref];
        [largeimage retain];
    }
};

//
ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
{
    NSLog(@"booya, cant get image - %@",[myerror localizedDescription]);
};

if(mediaurl && [mediaurl length] && ![[mediaurl pathExtension] isEqualToString:AUDIO_EXTENSION])
{
    [largeimage release];
    NSURL *asseturl = [NSURL URLWithString:mediaurl];
    ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
    [assetslibrary assetForURL:asseturl 
                   resultBlock:resultblock
                  failureBlock:failureblock];
}

}

Gaurav Patel
  • 957
  • 1
  • 6
  • 16