Objective-c – Reading Image from the Local Folder in Objective C

iphoneobjective c

I need to Read a Image from the specific URL .

It works fine with WWW . but it returns a nil when the URL pointing the Local Folder .

// Works 
NSString *sampleData = @"http://blogs-images.forbes.com/ericsavitz/files/2011/05/apple-logo2.jpg";

// Returns nil 
NSString *sampleData = @"USER/user2/...";

Note :
I am changing the NSString to NSURL and creating the UIImage .

NSURL *url = [NSURL URLWithString: data];

UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];

Best Answer

You are supplying a relative pathname for the file URL. That relative pathname is interpreted relative to the current working directory of the running application, which isn't guaranteed to be anything in particular, and so is almost certainly not what you want.

You can either supply an absolute path - one that starts with '/' - or set your app's current working directory to something explicit, like your user's Documents folder.

Related Topic