Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Image from the Local Folder in Objective 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]];
like image 565
Shadowtech Avatar asked Nov 05 '22 15:11

Shadowtech


2 Answers

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.

like image 105
Mike Crawford Avatar answered Nov 15 '22 00:11

Mike Crawford


you probably should have a look into the NSBundle Class. Methods like

- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)extension subdirectory:(NSString *)subpath

or

- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension

is probably what you want

like image 25
HeikoG Avatar answered Nov 15 '22 01:11

HeikoG