Adding custom fields to an images metadata dictionary - iphone

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!!!

Related

How to get Image Filename and Tag of images in photos album?

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];

iOS - Should we just save image as local file or as binary data (in Core Data object)? Which way is faster?

I'm currently use binary data saved in Core Data object to generate UIImage as code below.
UIImage * postImage = [UIImage imageWithData:image];
double imageRatio = postImage.size.height / postImage.size.width;
[imageContent setFrame:CGRectMake(imageContent.frame.origin.x, imageContent.frame.origin.y, imageContent.frame.size.width, imageContent.frame.size.height * imageRatio)];
[imageContent setImage:postImage];
However, I found there's a little bit slow when generate image.
Should I save image locally when retrieve data from server instead?
Which way is faster?
Thanks,
Save the image locally (e.g. in the Documents or Caches directory) instead of in the Core Data database. You get no benefit from storing binary data in the database as you will never search or index by this data, and it will just bloat your database. Just store an NSString reference to the filename/path you save the image to.
I imagine the UIImage:imageWithData method is slower when compared with reading an image from file that is already in a suitable image format.
save image locally and in your coreData save the location and name "URL" for that image
save your image locally such as PNG or JPEG instead of NSData.

iPhone how to send a large image along with a JSON file? Should I embed it within the JSON or send separately?

I'm trying to create a way to save the interface of my app in a JSON file. While static elements I can define by their frames and/or center points, the interface does include a single image selected from the photo library. Because some iPhones have 8MP cameras, I can anticipate that some images would be very large.
I would like to be able to save this interface and send it over by email, so another app user can re-open the file and see the same interface with the image included.
Currently I'm using JSONKit to save my data into a JSON object. This means that the email will contain a single JSON file containing both the layout parameters and the image. I've never worked with such large JSON files, so the question is - will having several megs of image data within JSON present parsing issues? Or should I select some other file container that would have : first part JSON file, second part raw image data and then separate that file into a JSON string and a raw data string?
This is what I'm using currently to get a JSON file. After adding NSData to JSON, the convert to json method returns nil
NSString* currentURL = webView.request.mainDocumentURL.absoluteString;
[dictionary setValue:currentURL forKey:#"webViewAddress"];
//this call correctly returns the json string
NSLog(#"%#", [dictionary JSONString]);
NSLog(#"+++++++++++++++++++++++++++++++++++++");
NSData* imageData = UIImagePNGRepresentation(arOverlayView.image);
//after this call, the json returns nil
[dictionary setValue:imageData forKey:kimageData];
NSLog(#"%#", [dictionary JSONString]);
If you are using email as a transport mechanism, your binary data will have to end up as something like Base64 encoded at some point in its life. There is no "raw" format for email, there is only text.
At best you could implement Base85 encoding which is more efficient than Base64
So you are going to have to suck up the large file sizes unless you invent your own transport mechanism - such as a point to point TCP/IP based link.

Change file names of images?

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!

CoreData : store images to DB or not?

I am making an app that takes photos from web site for some Username and shows it in a UITable with username then when clicking user name it shows photos for this user and then clicking to name of photo it shows full screen photo.
My question is I am using NSData to get photos from internet. Do I have to save the data to CoreData? When pressing name of user it creates NSData and downloads photos from internet and shows them on UITable. And it takes time.
What is good approach? and How can save this images to CoreData?
I am using this method
NSData *imageData=[flickr dataForPhotoID:firstPhoto.id fromFarm:firstPhoto.farm
onServer:firstPhoto.server withSecret:firstPhoto.secret inFormat:
FlickrFetcherPhotoFormatSquare];
and here definition of dataForPhotoID method
- (NSData *)dataForPhotoID:(NSString *)photoID fromFarm:(NSString *)farm
onServer:(NSString *)server withSecret:(NSString *)secret
inFormat:(FlickrFetcherPhotoFormat)format {
#if TEST_HIGH_NETWORK_LATENCY
sleep(1);
#endif
NSString *formatString;
switch (format) {
case FlickrFetcherPhotoFormatSquare: formatString = #"s"; break;
case FlickrFetcherPhotoFormatLarge: formatString = #"b"; break;
}
NSString *photoURLString = [NSString stringWithFormat:#"http://farm%#.static.flickr.com/%#/%#_%#_%#.jpg", farm, server, photoID, secret, formatString];
NSURL *url = [NSURL URLWithString:photoURLString];
return [NSData dataWithContentsOfURL:url];
}
First, always store your images in a usable format such as PNG or JPEG instead of NSData. This will save you a lot of headaches.
Second, the rule for storing binary data is:
< 100kb store in the same table as the relevant data
< 1mb store in a separate table attached via a relationship to avoid loading unnecessarily
1mb store on disk and reference it inside of Core Data
Update
The storage inside of Core Data should be binary and you can write accessor methods for it. Take a look at this answer: Core data images from desktop to iphone
Update
The example code I linked to describes how to create accessors in your NSManagedObject subclass that will convert the image back and forth between a UIImage and binary data.
You can simply store UIImage objects in CoreData directly, just use Transformable data type, and you are ready to go