我成功地将图像转换为base64字符串,但是当我服务于服务器时,服务器会获得损坏的数据 我正在使用http://codebeautify.org/base64-to-image-converter#来获取正确的图像输出
NSString *base64String=[UIImagePNGRepresentation(ProfileImageView.image)
base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];
[base64String stringByReplacingOccurrencesOfString:@"\n\r" withString:@""];
[base64String stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
NSString *post =[[NSString alloc] initWithFormat:@"UserId=%@&UserName=%@&Password=%@&Email=%@&MobileNo=%@&Profiledata=%@",[_DicData valueForKey:@"userId"],[_DicData valueForKey:@"userName"],[_DicData valueForKey:@"password"] ,[_DicData valueForKey:@"email"],MobileNumberLable.text,base64String];
NSString *strurl2=@"api/UpdateUserProfledata/";
NSString *strurl=[NSString stringWithFormat:@"%@%@",_Url1,strurl2];
NSURL *url=[NSURL URLWithString:strurl];
NSData *postData= [post dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *req=[[NSMutableURLRequest alloc]initWithURL:url];
NSString *authtoken=[NSString stringWithFormat:@"ios %@",[_DicData valueForKey:@"authToken"]];
[req setValue:authtoken forHTTPHeaderField:@"Authorization"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:postData];
NSHTTPURLResponse *urlResponse = nil;
NSError *err = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:&urlResponse error:&err];
NSLog(@"%@",[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding]);
当我从我身边转换图像时,我得到了 总字符= 133624和总字数= 0 但是当服务器转换得到它 总字符= 133624和总字数= 2891 帮助我!
.net
中图像转换器的后端代码public UserViewModel UpdateUserProfiledata(UserViewModel userupdate) { UserViewModel model = new UserViewModel(); if(userupdate!= null&& userupdate.UserId> 0) { var getuser = _unitOfWork.UserRepository.GetMany(x => x.UserId == userupdate.UserId).ToList(); if(getuser.Count()> 0) { foreach(getuser中的var项) { item.UserName = userupdate.UserName; item.Email = userupdate.Email; item.MobileNo = userupdate.MobileNo; if(!string.IsNullOrEmpty(userupdate.Profiledata)) { var img = GetBytes(userupdate.Profiledata); item.ProfilePhoto = img; } model.Profiledata = userupdate.Profiledata.Trim(); _unitOfWork.UserRepository.Update(项目); _unitOfWork.Save(); 回归模型; } } } 回归模型; } static byte [] GetBytes(string str) { byte [] bytes = new byte [str.Length * sizeof(char)]; System.Buffer.BlockCopy(str.ToCharArray(),0,bytes,0,bytes.Length); 返回字节; }
答案 0 :(得分:0)
NSData *dataImage=UIImageJPEGRepresentation(yourImage, 1);
NSString *base64String = [self base64forData:dataImage];
将此base64String
传递给您的请求中的服务器。
尝试使用base64编码:
- (NSString*)base64forData:(NSData*)theData
{
const uint8_t* input = (const uint8_t*)[theData bytes];
NSInteger length = [theData length];
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t* output = (uint8_t*)data.mutableBytes;
NSInteger i;
for (i=0; i < length; i += 3) {
NSInteger value = 0;
NSInteger j;
for (j = i; j < (i + 3); j++) {
value <<= 8;
if (j < length) {
value |= (0xFF & input[j]);
}
}
NSInteger theIndex = (i / 3) * 4;
output[theIndex + 0] = table[(value >> 18) & 0x3F];
output[theIndex + 1] = table[(value >> 12) & 0x3F];
output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '=';
output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '=';
}
return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] ;
}