1

After doing some research I am a little confused. I can grab a single image from a url if I know the exact directory for example,

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL  URLWithString:@"http://www.myUrl.com/pic/workaholics.png"]]];
UIImageView *view1 = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:view1];

This of coarse works fine, but what if you didn't know the exact path. In Linux you could always do some sort of recursive search but with iOS I can't figure it out. For example, in the "pic" directory that I specified in the latter case what if there was other directories called pic1,pic2,pic3 etc and each directory had an arbitrary number of png's. What I am having a hard time wrapping my head around is how can I recursively search the "pic" directory to extract all directories contents that match .png. I would of coarse then store this is NSData and convert to a string and then store the contents in an array. I'm very much confused at this point. Any help would be greatly appreciated.

3 Answers 3

1

Your difficulty here will be getting a directory listing from a web server - since you are retrieving the images over the web. Many web servers will not provide a directory listing as a security measure against hacking attempts.

if you control the web server, you can reconfigure it to provide a directory listing - then make an NSData object from the directory level URL (http://www.myUrl.com/pic/) - and parse this to pull out all the files within the directory. You can then request each individual picture and add it to an array yourself.

if you don't control the web server, but can write to the directory, you could achieve a similar implementation by creating a manifest file (XML-plist would be a good format) which lists the filenames contained in the directory. You'd need to update the manifest any time you added a new picture to the directory though.

otherwise - you have to do some pretty dirty/hacky things, like assuming there are no more than n pictures, all pictures have the filename "pic_m.png" and then iterating through a loop from 0 to n attempting to retrieve each file until you hit a failure.

Sign up to request clarification or add additional context in comments.

Comments

0

UIImage needs an explicit thing (URL or filepath) to get its data from.

One potential solution is to implement searching for your .png files via NSFileManager's enumeratorAtPath: method. The reference guide I've linked to even has a code fragment you can use to to create UIImages (just switch the @"doc" to @png and instead of doing scanDocument, do your UIImage * image = ... thing).

I'll leave it as an exercise to figure out how to do it recursively, but I hope my answer helps you out!

1 Comment

Wouldn't that only work with URLs of type file:? I believe the question mentions http: as the protocol.
0
NSURL *url = [NSURL 
URLWithString:@”www.ArticleDean.com\images\sample.jpg”];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] intiWithData:data]; 

try this one

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.