unable to save an NSMutableArray of image using persistent storage - objective-c

I am trying to store an NSMutableArray of images using persistive storage but it is not storing it
here is my code
[imagingList addObject:image ]; //image is of type UIImage
imagingHistory=[NSUserDefaults standardUserDefaults];
[imagingHistory setObject:imagingList forKey:#"imaging history" ];
and in my view did load I write
imagingList = [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:#"imaging history" ];
when I re-load the application the list of objects is set back to Zero
am I missing something ?

You can't save a UIImage directly in the user defaults. You have to convert it to an NSData first. Just by chance, when I was looking for the NSUserDefaults reference page to answer this question, a found a tutorial on how to do exactly what you want.

Hmm you really shouldn't be trying to save UIImages into NSUserDefaults. Take a look at this resource https://discussions.apple.com/thread/1729849?start=0&tstart=0 it explains how to get the NSData of the image and write it to a file. From there you can save a reference (NSURL or NSString) to the image's path in your app's document directory and you can load it from there using something like UIImage aImage = [UIImage imageWithData:[NSData datWithContentsOfURL:pathURL]].
Hope that helps.

Related

Load an image file to core data

I'm a new developer with objective c.
I have images stored in a folder on my computer and I want to insert it to my coredata, can someone please tell me how can I do so, cause I can't find the right way for this.
By the way its my first question so get mad on me :)
Thank you
You need an Entity:(b.e.: Photos), this entity need an attribute, with type is Binary Data. (b.e imageData.
Here the example to save and to show:
// Save the image.
UIImage *imageToSave;
NSManagedObject *newPhoto = [NSEntityDescription entityForName:#"Photo" inManagedObjectContext:yourManagedObjectContext];
NSData *imageData = UIImageJPEGRepresentation(imageEdited, 0.7);
[newPhoto setValue:imageData forKey:#"imageData"];
[yourManagedObjectContext save:&error];
// Sow the image
UIImage *image = [UIImage imageWithData:[newPhoto valueForKey:#"imageData"]];

Caching images with NSMutableDictionary

I am attempting to create an application that goes through various images from the net and aim to cache them onto the iPhone for offline use. The code I am currently working with is:
NSMutableDictionary *Cache;
- (UIImage *)CachedImage: (NSString*)url {
UIImage *image = [Cache objectForKey:url];
if (image == nil) {
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
[Cache setObject:image forKey:url];
//NSLog (#"Stored");
return image;
} else {
//NSLog (#"Taken");
return image;
} }
I call the function and place the image into an ImageView using the strip of code below.
[self.imageView setImage:[self CachedImage:url]]; // Change url to desired URL.
Using the NSLog, the problem I found is that the code doesn't actually store the value because the value is always reading nil. Why is that and are there other ways of storing images for offline use?
Thanks in advance.
-Gon
Use NSCache to cache UIImages. You can also save the image locally (if you reuse these images a lot and during multiple launch) so whenever your app closes or you flush your cache, you can get the images immediately from your local directory.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSCache_Class/Reference/Reference.html
You are looking for
NSCache
Check it out here: http://nshipster.com/nscache/
Poor NSCache, always being overshadowed by NSMutableDictionary in the
most inappropriate circumstances. It’s like no one knows its there,
ready to provide all of that garbage collection behavior that
developers take great pains to re-implement themselves.

Retrieving UIImage in byte form from NSArray - iOS

I have an array which contains a set of UIImage in byte forms. --My partner saved the UIImage in the database, and he created an array to retrieve the data-- I created this to retrieve the data from the array:
for (NSDictionary *row in arr){
NSData *imgData = [row objectForKey:#"ImageFile"];
When I put it in the NSLog, it gave the right data. However when I try to display the image using this:
UIImage *imgDis = [UIImage imageWithData:imgData];
It is giving me an error saying [__NSCFString bytes]: unrecognized selector sent to instance..
I honestly don't know what to do, so if anyone can help me it will be greatly appreciated.
As what #Safecase said in the comments, I decided to save the image path instead of the image itself. This way, I could just use UIImage imageWithContentsOfFile:imageFilePath to display the image. Thanks for the help!

How to use Lazy Loading in Paging?

I have a viewController, in that i have used Page control. and each page have 4 imageViews.
I have passed Xml and according to number of images in that i got the number of pages of pageControl that is (NumberOfImages/4).
I have done my code like this...
first i get url of each images and store them in Array..
for(int i=0;i<[Array Count];i++)
{
imgString=[NSString stringWithFormat:#"%#",[Array objectAtIndex:i]];
NSLog(#"string :%#",imgString);
NSString *abc=[imgString tringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//NSLog(#"abc :%#",abc);
NSURL *url=[NSURL URLWithString:abc];
NSLog(#"url :%#",url);
NSData *data = [NSData dataWithContentsOfURL:url];
[ImageArray addObject:[UIImage ImageWithData:data]];
}
The above code takes time to fetch images from URL.and until all r not fetched i have to wait..
So i want to use Lazy Loading Here..I have show apple Lazy Loading Example for TableView..but not getting how to use it with Paging??
so can anyone suggest me what can i do for this??
any Help is appreciated..
You could create a Category for the UIImageView class and add some methods that will allow you to do the following:
Load the image from a cache file in the tmp directory - if a cache file can't be found, download the image from the web on a background thread then save it to cache.
By checking for a cache file first you can skip re-downloading the content.
By downloading the image on a background thread your application will not lag while waiting for content to finish loading.
By using a Category, your methods will apply to any UIImageView you want to use - and will even work in paging.
Rather than post a complete code solution here I'm going to suggest that you research and develop your own solution.
Best of luck.

PDF writing from iPad

I am able to generate a PDF with the following code. Can someone please help me with how I would add text to this? Thanks in advance.
-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[aView drawRect:aView.bounds];
// remove PDF rendering context
UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(#"documentDirectoryFileName: %#",documentDirectoryFilename);
}
I've never done this (yet, I plan getting on it tomorrow). But I think the right place to go is the Quartz 2D programming guide. It's also available from your developer documentation if you need it offline.
Basically you the same as you posted but appart from calling drawRect: you perform all the text drawing you want on that context.
There's useful information on writing simple strings on this guide
I hope it helps. It sure helped me!