Is it possible to use NSKeyedArchiver to store an animated UIImage?

796 views Asked by At

I loaded an image using frames downloaded from a web server.

NSArray* frames = [self fetchFramesFromServer];
imageView.image = [UIImage animatedImageWithImages:frames duration:1.0];

Whenever I try to archive these frames, the encoding function returns NO:

BOOL success = [NSKeyedArchiver archiveRootObject:(frames) toFile:archivePath];

Whenever I try to archive the image, the encoding function also returns NO:

BOOL success = [NSKeyedArchiver archiveRootObject:(imageView.image) toFile:archivePath];

Is this even possible, or am I missing some caveat of archiveRootObject that requires the UIImage to be loaded from the bundle, in png-format, or non-animated?

Just FYI, the archivePath when printed out in the debugger is

NSPathStore2 * @"/var/mobile/Containers/Data/Application/E475E3C1-4E3F-43D6-AD66-0F98320CF279/Library/Private Documents/assets/a/b/c.archive"   0x000000012c555a60

UPDATE:

I am storing the individual frames of the animation inside a for-loop, but I get the following:

+ (void) saveImage:(UIImage*)inputImage
    withSuffix:(NSString*)fileSuffix
{
if (inputImage == nil) {
    NSLog(@"ERROR: input image is nil!");
    return;
}

// strip away intermediate folders to avoid over-nesting
// ex: __F1/MF/FR/1_MF_FR_Animation_a1.png
// ===> 1_MF_FR_Animation_a1.png
NSString* suffixStub = [MyDatabase stubOfFilepath:fileSuffix];

NSURL* url = [MyDatabase folderURL];
NSURL* imageURL = [url URLByAppendingPathComponent:suffixStub];

NSData* pngData = UIImagePNGRepresentation(inputImage);
[NSKeyedArchiver archiveRootObject:pngData toFile:imageURL.path];
pngData = nil;

NSLog(@"Image %@ successfully saved!", fileSuffix);
} 

But on the NSKeyedArchiver line, I get "malloc: *** error for object 0x178202340: Freeing unallocated pointer"

1

There are 1 answers

1
Vishnu gondlekar On

First convert your image to NSData and then try archiving it

NSData* data = UIImagePNGRepresentation(imageView.image);
BOOL success = [NSKeyedArchiver archiveRootObject:data toFile:archivePath];

In case of NSArray, archiving is possible only if all the objects of array conforms to NSCoding protocol. If your array has custom object refer Custom object archiving on how to conform to NSCoding.

UPDATE

If objects of your array are frames of type CGRect the you can use NSStringFromCGRect() and CGRectFromString() to store and then retrieve them.