Muhammad Umar
Muhammad Umar

Reputation: 11782

Convert ImagePath in DocumentDirectory to NSURL Iphone

I am using following code

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 is

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

however i get Nil igImageHookFile NSURL. What is wrong?

Upvotes: 1

Views: 440

Answers (2)

Nitin Gohel
Nitin Gohel

Reputation: 49730

Use fileURLWithPath for converting your Path of Document Directory to NSURL like this in piece of code:

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

For Example:

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

OUTPUT of path URL something like this:-

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

Upvotes: 5

Armaan Stranger
Armaan Stranger

Reputation: 3130

Try this:

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

Hope it Helps!!

Upvotes: -1

Related Questions