-2

这是网址

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

我想得到:

CF2AF034-9CF7-4472-9185-5EEFA1614A07

我该怎么做?NSURL 中是否有可以实现此目的的方法?

这就是我所做的:

-(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];

结果

(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

但这没有意义。很明显 self 是 47 长度,我正在寻找只有 3 长度的 pre 。那么 preRange 怎么会是 [0,47] 呢?

4

2 回答 2

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];
}

}

于 2013-02-25T05:07:20.673 回答
0

下面的代码块可能会做你所追求的

__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);

但是,它不是很安全。它对格式正确的 URL 做出假设,并且query数组中将有(至少)2 个值。

于 2013-02-25T04:56:56.430 回答