2011-10-01 17 views
6

Non riesco a salvare un'immagine utilizzando NSUserDefaults e crash la mia app e ho classe personalizzata utilizzato anche UserInfo che ha usatoiPhone: come posso archiviare UIImage usando NSUserDefaults?

-(void)encodeWithCoder:(NSCoder *)encoder 
{ 
    NSData *imageData = UIImagePNGRepresentation(categoryImage); 
    [encoder encodeObject:categoryName forKey:@"name"]; 
    [encoder encodeObject:imageData forKey:@"image"]; 
} 

- (id) initWithCoder: (NSCoder *) decoder e userInfo.categoryImage è variabile UIImage.

motivo: '- [UIImage encodeWithCoder:]: selettore non riconosciuto inviato ad esempio 0x7676a70'

userInfo = [[UserInfo alloc] init]; 

userInfo.categoryName = categoryName.text; 
NSLog(@"Category Name:--->%@",userInfo.categoryName); 

userInfo.categoryImage = image; 

appDelegate.categoryData = [[NSMutableDictionary dictionaryWithObjectsAndKeys: 
           userInfo.categoryName, @"name",userInfo.categoryImage,@"image", nil] mutableCopy]; 


[appDelegate.categories addObject:appDelegate.categoryData]; 
NSLog(@"Category Data:--->%@",appDelegate.categories); 

[appDelegate.categories addObject:userInfo.categoryName]; 
[appDelegate.categories addObject:userInfo.categoryImage]; 

[self saveCustomObject:userInfo.categoryName]; 

[self saveCustomObject:userInfo.categoryImage]; 


-(void)saveCustomObject:(UserInfo *)obj 
{ 
    appDelegate.userDefaults = [NSUserDefaults standardUserDefaults]; 
    NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:obj]; 


    [appDelegate.userDefaults setObject:myEncodedObject forKey:@"myEncodedObjectKey"]; 
} 

Così Please help me come posso memorizzare e recuperare UIImage utilizzando nsusedefaults ???

+0

Il saveCustomObject difficilmente può funzionare, perché memorizza ogni oggetto dato per la stessa chiave. – Davyd

risposta

21

UIImage non implementa il protocollo NSCoding, questo è il motivo per cui si ha l'errore.

Store:

NSData* imageData = UIImagePNGRepresentation(image); 
NSData* myEncodedImageData = [NSKeyedArchiver archivedDataWithRootObject:imageData]; 
[appDelegate.userDefaults setObject:myEncodedImageData forKey:@"myEncodedImageDataKey"]; 
} 

Restore:

NSData* myEncodedImageData = [appDelegate.userDefaults objectForKey:@"myEncodedImageDataKey"]; 
UIImage* image = [UIImage imageWithData:myEncodedImageData]; 
+0

Come posso usare negozio nel mio codice di cui sopra si prega di aiutare –

+0

rendere la classe 'UserInfo' implementare il protocollo 'NSCoding' e archiviare l'intero oggetto 'userInfo'. Leggi questo post di maggiori informazioni: http://stackoverflow.com/questions/3715038/how-to-store-a-nsuinteger-using-nscoding per esempio. – Davyd

+0

Sono abituato anche NSCoding ma anche applicazione è caduta –

34

Se fossi in te, io salverò l'UIImage in un file (usare il [hash myImage], per generare un nome di file) e salvare il nome del file come NSString utilizzando NSUserDefault ;-)

+9

Per l'amor del cielo, si prega di accettare questo come risposta. Salvare un UIImage su NSUserDefaults è davvero un'idea stupida. – Mugunth

10

Immagine Salva NSUserDefaults:

 [[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(image) forKey:@"image"]; 

Immagine recuperare il NSUserDefaults:

NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"]; 
    UIImage* image = [UIImage imageWithData:imageData]; 
Problemi correlati