2

I'm trying to display an image from my Database.

This part works nicely. I save the image in the DB like this :

UIImage *resizedImg = [Generics scaleImage:self.photo.image toResolution:self.photo.frame.size.width and:self.photo.frame.size.height];
NSData *imgData = UIImageJPEGRepresentation(resizedImg, 0.9);
NSString *stringDataImage = [NSString stringWithFormat:@"%@",imgData];
[dict setObject:stringDataImage for key@"image"];
// POST Request to save the image in DB ...

Now when I try to load the image and set it into my UIImageView I do this way :

NSData *data = [[[MyUser sharedUser] picture] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:data]];

Or

NSData *data = [[[MyUser sharedUser] picture] dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
self.imageView.image  = [UIImage withData:data];

But this doesn't work.

data is not equal to imgData, I think it's the encoding but I can find the good one. And [UIImage withData:data] return nil;

Can you help me?

EDIT :

Convert and save

UIImage *resizedImg = [Generics scaleImage:self.photo.image toResolution:self.photo.frame.size.width and:self.photo.frame.size.height];
NSData *imgData = UIImageJPEGRepresentation(resizedImg, 0.9);
[dict setObject:[imgData base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength] forKey:@"image"];

Load the image

NSData *data = [[NSData alloc] initWithBase64EncodedData:[[MyUser sharedUser] picture] options:kNilOptions];
NSLog(@"%@", data);
self.image.image = [UIImage imageWithData:data];
4

2 回答 2

5

You are converting the NSData to a string and saving that to your database. Two issues:

  1. Your choice of using the stringWithFormat construct is an inefficient string representation of your data (resulting in string representation that is roughly twice the size). You probably want to use base64 (for which the string representation is only 33% larger).

  2. You are saving your string representation, but never converting it back to binary format after retrieving it. You could write a routine to do this, but it's better to just use established base64 formats.

If you want to save the image as a string in your database, you should use base64. Historically we would have directed you to one of the many third party libraries (see How do I do base64 encoding on iphone-sdk?) for converting from binary data to base64 string (and back), or, iOS 7 now has native base 64 encoding (and exposes the private iOS 4 method, in case you need to support earlier versions of iOS).

Thus to convert NSData to NSString base64 representation:

NSString *string;

if ([data respondsToSelector:@selector(base64EncodedStringWithOptions:)]) {
    string = [data base64EncodedStringWithOptions:kNilOptions];  // iOS 7+
} else {
    string = [data base64Encoding];                              // pre iOS7
}

And to convert base64 string back to NSData:

NSData *data;

if ([NSData instancesRespondToSelector:@selector(initWithBase64EncodedString:options:)]) {
    data = [[NSData alloc] initWithBase64EncodedString:string options:kNilOptions];  // iOS 7+
} else {
    data = [[NSData alloc] initWithBase64Encoding:string];                           // pre iOS7
}
于 2013-11-05T15:26:46.633 回答
2

If you really want to turn binary data into a string you should be using base64 encoding. Luckily for you, NSData now supports Base64 natively

So you could get your string from data with:

NSString *stringDataImage = [imgData base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];

And you could turn this back into NSData with:

NSData *imgData = [[NSData alloc] initWithBase64EncodedString:stringDataImage options:kNilOptions];
于 2013-11-05T15:29:56.350 回答