圖片轉成NSDATA然後BASE64編碼POST到伺服器
阿新 • • 發佈:2019-02-15
//圖片轉換成data
NSData *imageData = UIImagePNGRepresentation(ima);
NSString *dataStr = [NSString stringWithFormat:@"%@", [imageData base64EncodedDataWithOptions:0]];
NSLog(@"ImageData == %@", dataStr);
這些方法可以幫助你輕易的將NSData物件轉化為Base64,或者將Base64轉化為NSData object。見以下的例子:
NSData* sampleData = [@"Some sample data" dataUsingEncoding:NSUTF8StringEncoding];
NSString * base64String = [sampleData base64EncodedStringWithOptions:0];
NSLog(@"Base64-encoded string is %@", base64String); // prints "U29tZSBzYW1wbGUgZGF0YQ=="
NSData* dataFromString = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSLog(@"String is %@",[NSString stringWithUTF8String:[dataFromString bytes]]); // prints "String is Some sample data"