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!
Related
Is there a way to get the image file name and tag of images in the photo album?
I'm using ALAssetsLibrary and still scratching...
You can copy the ALAssetLibrary object into an array (or better in a Local DB). Make objects of ALAsset to display and give them tag (depending on array index).
Or otherwise if you still wish to continue with the Image names. you can obtain them from the UIImagePickerControllerReferenceURL. Try the substring function to get the name (hint: mostly it is before the &ext, also you can get the extension of file.)
ALAssetRepresentation *AssetRepObj = [AssetObj defaultRepresentation];
NSString *FileName = [AssetRepObj filename];
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
I am populating a standard UITableViewCell with an image as follows:
cell.imageView.image = [UIImage imageNamed:#"someImageName.png"];
Is there a way to have "someImageName.png" be a random png image within my Xcode project? For the record, what I'd really like to do is put similar images in their own directory within the project - e.g. pets, faces, etc, and then depending on the table row select a random image from that directory.
I know I could rename the images using a scheme such as "image_0.png", "image_1.png" ... "image_n.png" and then use a random number x between 0..n to create "image_x.png" - but I'm curious if there's a way just to retrieve a random .png image file without doing the renaming (or writing the files to local storage, then retrieving a random file from there)?
Thanks very much.
You'll need to use the - (NSArray *)pathsForResourcesOfType:(NSString *)extension inDirectory:(NSString *)subpath; method of NSBundle class.
Then just take one random path from that array and get the image with +(UIImage*)imageWithContentsOfFile::(NSString*)fileName; or +(UIImage*)imageWithData:(NSData*)imageData; and the +(NSData*)dataWithContentsOfURL:(NSURL *)url;
I have an app with a large number of images (1000+). I have a database table which contains all the filenames for these images. Images are loaded on demand based on this filename using UIImage* image = [UIImage imageNamed:...]
Due to the large number of images, I would like to programmatically test to ensure all image that are included in the database are in fact present in the project. To achieve this, I'm pulling all filenames from the table, looping over each filename, running the above code, and checking to see if image != nil. This works just fine.
The problem is that I would like to confirm that both normal resolution and high resolution (#2x) images are there. If the high-resolution file is present but the normal-resolution file is not, my code will not detect this.
Is there some way I can achieve this without having the run this process twice, once per resolution type? Can I force the SDK to ignore #2x files?
You should just use NSFileManager. It will probably be much quicker since it won't actually load the contents of the image files. For each fileName, use NSFileManager's fileExistsAtPath method to check for the image. Then, append "#2x" to the base name to check for the 2x image.
I would use NSBundle to locate the files so the UIImage doesn't have to be loaded into memory, you could use something like...
NSBundle *myBundle = [NSBundle mainBundle];
if([myBundle pathForResource:#"MyImage" forType: #"png"] == nil){
// low res image isn't there
}
if([myBundle pathForResource:#"MyImage#2x" forType: #"png"] == nil){
// high res image isn't there
}
Should be much faster...
Is it possible to add custom dictionary fields to an images metadata?
The following statement
writeImageDataToSavedPhotosAlbum:metadata:completionBlock:
Allows images to be added with metadata.
Is this dictionary (the metadata dictionary) treated as a normal dictionary?
I mean, Is it possible to add custom fields to it.
Of course the original metadata dictionary needs to be copied, but once this is done can it be edited and used.
Also when would this be done? If an image is captured with the iphone and I want to add stuff to the metadata, will this be writing an image twice, or is the first captured images metadata available, modified and then the image is saved with the metadata?
Yes it is possible to add custom distionary fields to image metadata. You can use unused dictionary keys to write your data within the image metadata. Just u have to do is when u capture the image and get it through image picker, just save it at a location and obtain it's metadata by using :
NSData *imgData = UIImageJPEGRepresentation(imageView.image, 1);
CGImageSourceRef source = CGImageSourceCreateWithData((CFMutableDataRef)imgData, NULL);
NSDictionary *metadata = [(NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source,0,NULL)autorelease];
Then edit image metadata and save image by using :
writeImageDataToSavedPhotosAlbum:metadata:completionBlock:
It will save the new image with metadata inside the iPhone photo library then u can delete the old image if u want.
Hope it might help!!!