简体   繁体   English

是否可以使用NSKeyedArchiver存储动画的UIImage?

[英]Is it possible to use NSKeyedArchiver to store an animated UIImage?

I loaded an image using frames downloaded from a web server. 我使用从Web服务器下载的帧加载了图像。

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

Whenever I try to archive these frames, the encoding function returns NO: 每当我尝试存档这些帧时,编码函数都将返回NO:

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

Whenever I try to archive the image, the encoding function also returns NO: 每当我尝试存档图像时,编码功能也会返回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? 这是否有可能,还是我错过了archiveRootObject的一些警告,要求从捆绑包中加载png格式或非动画化的UIImage?

Just FYI, the archivePath when printed out in the debugger is 仅供参考,在调试器中打印出来时的archivePath是

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: 我将动画的各个帧存储在一个for循环中,但是得到以下信息:

+ (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" 但是在NSKeyedArchiver行上,我收到“对象0x178202340的malloc:***错误:释放未分配的指针”

First convert your image to NSData and then try archiving it 首先将映像转换为NSData,然后尝试将其归档

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. 对于NSArray ,仅当array的所有对象均符合NSCoding协议时才可以进行归档。 If your array has custom object refer Custom object archiving on how to conform to NSCoding . 如果您的数组具有自定义对象,请参阅自定义对象归档以了解如何符合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. 如果数组的对象是CGRect类型的框架,则可以使用NSStringFromCGRect()CGRectFromString()进行存储,然后检索它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM