I used this answer to convert a UIImage in a NSSstring,
https://stackoverflow.com/a/11251478/3741799
to encode
- (NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
to decode
- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];
}
here encode the image and put the NSString in an NSObject class
self.newData.imageString = [self encodeToBase64String: image];
add the new object to the tableView
- (Void) insertNewObject: (id) sender {
if (! self.objects) {
[self.objects addObject: self.newData.imageString];
[self.tableView reloadData];
}
[self.objects InsertObject: self.newData atIndex: 0];
NSIndexPath * indexPath = [NSIndexPath indexPathForRow: 0 inSection: 0];
[self.tableView insertRowsAtIndexPaths: @ [indexPath] withRowAnimation: UITableViewRowAnimationAutomatic];
}
then load the new object in the TableView!
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {
static NSString * CellIdentifier = @ "Custom";
CustomCell * cell = (CustomCell *) [self.tableView dequeueReusableCellWithIdentifier: CellIdentifier forIndexPath: indexPath];
ObjectClass * object = [self.objects objectAtIndex: indexPath.row];
cell.imageView.image = [self decodeBase64ToImage: object.imageString];
return cell;
}
But when I load the image in the cell can not decode the string because it has a null value, and the app crash!
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[_NSPlaceholderData initWithBase64EncodedString:options:]: nil string argument'
I think that the problem is in the encoding but I can't solve it by myself!
Where am I wrong?
thanks
QUESTION UPDATE
The Zaph question is right, I try to explain better: I have an empty table view, the user adds an image and a text string for each row. So I tried to use this guide official Apple
In the class XYZToDoItem add the property @property NSString * itemName; and I can easily add the text string created by user. But how can I add the images in this class? So I thought to convert images into strings and add a property @property NSString * imageString; But something wrong and I can't do that ! Can you suggest a better way to do this?
Thank you so much for your support and help !!