将DocumentDirectory中的ImagePath转换为NSURL Iphone

时间:2013-09-13 07:27:17

标签: iphone objective-c

我正在使用以下代码

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSURL *myUrl = [[NSURL alloc] initWithString:self.selectedRing.ringThumbNailImagePath];
[Utilities responseDataFromURL:myUrl completionBlock:^(NSData *fileData, NSError *err)
{
    if (err != nil)
    {
        [self.indicator stopAnimating];
        [Utilities errorDisplay:@""];
    }
    else
    {
        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Test.igo"];
        NSLog(@"FilePath: %@", filePath);

        if ([fileData writeToFile:filePath atomically:YES])
        {
            CGRect rect = CGRectMake(0 ,0 , 0, 0);    
            NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@", filePath]];
            NSLog(@"JPG path %@", filePath);

filePath是

Users/umar/Library/Application Support/iPhone Simulator/6.1/Applications/B45223CF-B437-4617-A02D-DCA91C965A5A/Documents/Test.igo

然而我得到Nil igImageHookFile NSURL。有什么问题?

2 个答案:

答案 0 :(得分:5)

使用fileURLWithPath将您的文档目录路径转换为NSURL,如下所示:

NSURL *myImagePath = [NSURL fileURLWithPath:filePath]; //here filePath is your document directory full path with extantionm

例如:

NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Test.igo"];
        NSLog(@"FilePath: %@", filePath);

        if ([fileData writeToFile:filePath atomically:YES])
        {
            CGRect rect = CGRectMake(0 ,0 , 0, 0);   

            NSURL *igImageHookFile = [NSURL fileURLWithPath:filePath]; 

            NSLog(@"URL from path %@", igImageHookFile);

路径网址的输出如下: -

file://localhost/Users/umar/Library/Application Support/iPhone Simulator/6.1/Applications/B45223CF-B437-4617-A02D-DCA91C965A5A/Documents/Test.igo

答案 1 :(得分:-1)

试试这个:

NSURL *url = [NSURL URLWithString:[Request stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

希望它能帮助!!