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.
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.
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!
NSURL *url = [NSURL
URLWithString:#”www.ArticleDean.com\images\sample.jpg”];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] intiWithData:data];
try this one
Related
I'm saving some NSData with writeToFile:atomically: and then want to use that file very shortly afterwards. What is the easiest way to get the NSURL of the file I just saved?
[NSData writeToFile: atomically:] has a path parameter.
Just make the path parameter into a file URL and save that as an instance variable or property and you'll be all set.
Or even better, use [NSData writeToURL: atomically:] (i.e. convert your path into a URL to start with) and save that as a property or instance variable to be used later on.
You can convert a path into a file URL via [[NSURL alloc] initFileURLWithPath:].
And lastly, the real answer to the question you're trying to ask is: no, you can not divine the path or URL from an arbitrary "NSData" object. You'll have to save that information separately or alongside your data in order to keep track of where it came from.
I'm using NSCoding to manage a custom object with a few different fields, one of them images. I'm only targeting iOS 5.0+ and have switched to ARC. I have everything working but am focused on performance--I haven't seen a question like this asked, so here it goes:
I transform the UIImage into NSData and add it to the main NSCoding file (a plist, if it matters) for storage on the disk. If there is more than one image, the image names become sequential (e.g. image1, image2, image3.) I then use the image both in a UITableView (as a resized thumbnail) and in a detail view. The negative side to this is that the plist balloons in size, which means slow initial load times when I use it because it's loading all of the NSData at once.
What is the best way to eliminate this problem and only force the loading of one image at a time?
What I've thought of:
I write the NSData to the disk, add an array to the plist, and add only a reference to the filename of each image to the array. I suppose I'd then reference the image filename at specified position, find it on the disk, and use it?
Any and all thoughts would be most welcome. I'm more stuck on the conceptual implementation than anything else and, funnily enough, this is not an oft-discussed topic.
Thanks,
EDIT:
As requested below, here's an example of taking an image and turning it into NSData:
UIImage *originalImage;
NSData *imageData = UIImagePNGRepresentation(originalImage);
//I save it all to the app's document directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//I am using the images in a tableView and thus found it easiest to append the row number to each image filename.
//'y' below is just an integer that corresponds to the number of items in the master array
NSString *fileName = [NSString stringWithFormat:#"image%d.png",y];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:fileName];
[imageData writeToFile:documentsDirectory atomically:YES];
// NSLog(#"The filename is %#", fileName);
//newObject is an instance of my NSCoding object
[newObject setImageName: fileName];
Your suggestion is sound. Imagine a relational database. I would never save the images in one field as a blob. A filesystem is a very good place to save binary date in large amounts. It also gives you easy ways to duplicate files and so on.
So saving a reference in your plist will make parsing really fast and lazy loading an easy task to process images only when you need them.
iOS Programming: The Big Nerd Ranch Guide has a perfect example of this solution in chapter 14 of the 3rd Edition. Just download the code that is accessible from their site for a completed solution.
iOS Programming 3rd edition
Download Solutions
In my application I need to capture image as well as save it on local library and on server using FTP. Now I need to follow a format for image name while saving it on the server. I am able to capture and save the image on local library. But I am unable to find any method to change the name of the Image. Suppose I need to rename it as Productname-UserId.png Is there any way? Kindly help.
Thank you.
UIImagePNGRepresentation(UIImage*) is likely what you're looking for. You can save a UIImage as a PNG file in the Application Documents folder, then upload that to a server. The code to do this is quite trivial, so if you could post the code you're trying to use, that would be helpful to understanding, and recommending a solution for you.
Here's a short clipping from my code that does exactly this:
UIImage * image; // Some image you want to send
NSString * docDirWithSlash = [[self applicationDocumentsDirectory] stringByAppendingString:#"/"];
NSString * pngFile = [docDirWithSlash stringByAppendingString:file]; // <-- Change the string "file" to reflect the name you want.
[UIImagePNGRepresentation(image) writeToFile:pngFile atomically:YES];
// Send pngFile to the server here
Where applicationDocumentsDirectory looks like this:
- (NSString *) applicationDocumentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
return documentsDirectoryPath;
}
The real question here is where do you want the file name to be specific? On the device or on the server. In case of device use Charles answer, otherwise you should look at http protocol(file upload part). Actually it doesn't matter what name the file have on you local device, as when you send it to the server over http, you can provide any name. The tricky part here is that the server you are uploading to should take that parameter into account when saving that file. So if you are uploading to a server that you don't have hand on and it does some self naming convention - you probably stuck. If it's yours - look at how you're saving files on the server side and if you are taking in account that "filename" parameter...P.S. And don't forget to pass that argument to the upload request :)
I am new to iphone programming. I have a scenario in which I am creating a group inside a group. that is, in project I have created
Resources->group1
in group 1 I am storing png files and I want to take all those files in one array.
Please suggest me a way to do this.
Not sure for getting all image in an array in one shot , But you could get individual images by their name and add them in NSMutableArray of UIImages
Use below to get a image by name
UIImage* myImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:myImage.png ofType:nil]];
I want to change the file names of images in my app, can i do this?
If the image is inside your NSBundle, then the answer is No. If you copy an image from ALAssetsLibrary you can name it whatever you like.
If it is in your NSBundle and you'd like to create a copy of the same image name, you can do that by loading the NSData and saving it with a different name. That just uses up extra space though.
Why are you trying to change the name of an image to begin with? If you needed to, you can create an NSMutableDictionary and a special function which takes a "filename" and returns the name of the actual image file in your NSBundle.
For example if you had a file named "MyImage.png" which part of your program thinks is actually called "ThatCoolPicture.png", you can load it like this:
UIImage *img = [UIImage imageNamed:[self nameForPseudoname:#"ThatCoolPicture.png"]];
In this case nameForPseudoname takes a "nickname" and returns the real name.
Hope this helps!