2013-10-16 193 views
0

我一直在解碼編碼圖像時遇到麻煩。解碼iOS base64圖像

我用下面的代碼來編碼從UIImagePickerController我的圖像返回:

NSData *imageData2 = UIImageJPEGRepresentation(image, 1.0); 
    NSString *encodedString = [imageData2 base64Encoding]; 

我試過,圖像尺寸600×600來,我也試圖壓縮圖像。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *image = [self imageWithImage:[info objectForKey:UIImagePickerControllerEditedImage] scaledToSize:CGSizeMake(600, 600)]; 

    imgPreviewSelected.image = [self imageWithImage:image scaledToSize:CGSizeMake(600, 600)]; 

    CGFloat compression = 0.9f; 
    CGFloat maxCompression = 0.1f; 
    int maxFileSize = 250*1024; 

    NSData *imageData = UIImageJPEGRepresentation(image, compression); 

    while ([imageData length] > maxFileSize && compression > maxCompression) 
    { 
     compression -= 0.1; 
     imageData = UIImageJPEGRepresentation(image, compression); 
    } 

    image = [UIImage imageWithData:imageData]; 

    imagePickerReturnedImage = image; 

    NSData *imageData2 = UIImageJPEGRepresentation(image, 1.0); 
    NSString *encodedString = [imageData2 base64Encoding]; 

    [utils postData:@"example.com" : [NSString stringWithFormat:@"image=%@", encodedString]]; 

    [self dismissViewControllerAnimated:YES completion:NULL]; 
} 

當我嘗試解碼發送到我的數據庫的圖像時,它告訴我文件已損壞。

我試着用PHP和以下網站對其進行解碼: http://www.motobit.com/util/base64-decoder-encoder.asp

我的MySQL數據庫告訴我,該圖像爲1MB,我認爲這是對的600×600的圖像相當大。沒有壓縮,它是1,3MB。

我用這個方法來擴大我的形象:

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { 
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); 
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 
} 

我希望有人能幫助我,THX

回答

0

我用另一種方式的base64沒有,所以不需要答案。

+1

你用什麼方法,圖像尺寸? – ZeMoon

0

你的代碼看起來不錯。 你有沒有嘗試複製你的base64string在該網站解碼?

您可以檢查由

NSLog(@"dataSize: %@",[NSByteCountFormatter stringFromByteCount:data.length countStyle:NSByteCountFormatterCountStyleFile]);