0

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

https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/ThirdTutorial.html

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 !!

Community
  • 1
  • 1
Ragazzetto
  • 622
  • 1
  • 8
  • 18
  • 2
    Why are you converting the `NSData` to a Base64 encoded `NSString` and back? What are you trying to accomplish with this encoding/decoding? – zaph Oct 20 '14 at 00:33
  • in `cellForRowAtIndexPath:` does `self.objects` exist? In your `insertNewObject:` method you have an if which checks if self.objects is nil `if (! self.objects) {` and if it is, it calls `addObject:` on that nil. I guess you wanted to create `self.objects` first and then call `addObject:`. – Bartek Chlebek Oct 20 '14 at 00:33

1 Answers1

1

The code inside the conditional

if (! self.objects) { // true for self.objects == nil

    [self.objects addObject: self.newData.imageString]; 

    [self.tableView reloadData]; 
}

will only execute if self.objects is nil. So self.tableView reloadData is called when self.objects doesn't exist, meaning

ObjectClass * object = [self.objects objectAtIndex: indexPath.row]; // object is nil

will assign object to nil.

Then the subsequent call to decodeBase64ToImage will fail because object.imageString is nil.

Morgan Chen
  • 1,137
  • 7
  • 18