ios uiimageview nsdata nskeyedarchiver nscoder

ios - Desarchivar UIImageView con subvistas



nsdata nskeyedarchiver (1)

[Respuesta anterior eliminada.]

EDITAR: Hmm. Acabo de probar esto:

- (IBAction)doButton1:(id)sender { theData = [NSKeyedArchiver archivedDataWithRootObject:iv]; [iv removeFromSuperview]; iv = nil; } - (IBAction)doButton2:(id)sender { iv = [NSKeyedUnarchiver unarchiveObjectWithData:theData]; [self.view addSubview:iv]; }

Funcionó, con un UIImageView ordinario. Así que ahora supongo que no entiendo a qué te refieres; parece que un UIImageView ya está archivado con su imagen. Entonces debes tratar de resolver algún otro problema. Pero no entiendo cuál es el problema.

Me estoy volviendo un poco loco al intentar archivar y descomprimir un UIImageView que tiene varias subvistas que son vistas personalizadas derivadas de UIImageView. Esto es lo que hice:

Agregue una categoría al proyecto para tener en cuenta el hecho de que UIImage no se ajusta a NSCoding:

#import "UIImage-NSCoding.h" #define kEncodingKey @"UIImage" @implementation UIImage(NSCoding) - (id)initWithCoder:(NSCoder *)decoder { if ((self = [super init])) { NSData *data = [decoder decodeObjectForKey:kEncodingKey]; self = [self initWithData:data]; } return self; } - (void)encodeWithCoder:(NSCoder *)encoder { NSData *data = UIImagePNGRepresentation(self); [encoder encodeObject:data forKey:kEncodingKey]; } @end

El UIImageView que estoy archivando es una propiedad de mi controlador de vista principal llamado "fondo". Lo guardo así:

NSData *dataToSave = [NSKeyedArchiver archivedDataWithRootObject:self.background]; [dataToSave dataWithContentsOfFile:imagePath options:NSDataWritingAtomic error:nil];

Luego, más tarde, lo desarchivo de esta manera:

NSData *savedData = [NSData dataWithContentsOfFile:imagePath]; UIImageView *restoredImageView = [NSKeyedUnarchiver unarchiveObjectWithData:savedData]; self.background.image = restoredImageView.image; // this works - archived image is displayed

Ahora quiero mostrar mis subvistas (¡o una de ellas al menos!) Junto con el objeto raíz no archivado:

NSLog(@"Subviews: %d", [restoredImageView.subviews count]); // this tells me my subviews have been archived along with the root object NSLog(@"Subview 0: %@", [restoredImageView.subviews objectAtIndex:0]); // and they exist as expected NSLog(@"Subviews: %d", [self.background.subviews count]); // at this point no subviews are present [self.background addSubview:[restoredImageView.subviews objectAtIndex:0]]; // subview is not visible on screen NSLog(@"Subviews: %d", [self.background.subviews count]); // count is one, but where is it? ORUImageView *oru = [[ORUImageView alloc] init]; // I try creating the subview myself oru = [self.background.subviews objectAtIndex:0]; [self.background addSubview:oru]; // it still doesn''t show on screen [self.background bringSubviewToFront:oru]; // makes no difference NSLog(@"String: %@", oru.testString); // this correctly logs the test string I added to my custom class

Esto es lo que agregué a mi subclase ORUImageView:

- (void)encodeWithCoder:(NSCoder *)aCoder { [super encodeWithCoder:aCoder]; [aCoder encodeObject:self.testString forKey:@"theString"]; [aCoder encodeObject:self.image forKey:@"theImage"]; // not sure if this is needed } - (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; [self setTestString:[aDecoder decodeObjectForKey:@"theString"]]; [self setImage:[aDecoder decodeObjectForKey:@"theImage"]]; // not sure if this is needed return self; }

Lo intenté con y sin la codificación de la propiedad de la imagen.

Sospecho que el problema está relacionado con esta propiedad, ya que claramente estoy agregando la subvista a la vista no archivada, ¡simplemente no está visible! Siento que la propiedad de la imagen no se está configurando, pero no puedo encontrar la manera de configurarlo, y no puedo encontrar nada que me diga la forma correcta de hacerlo.