如果要以字符串形式上传图像,则必须将图像数据转换为base64,然后可以使用上述方法:
要转换为base64字符串,请参阅此答案。然后,您可以使用您的代码进行上传。
其他方式
您可以像这样直接上传图片:
-(void)uploadImage{NSData *imageData = UIImagePNGRepresentation(yourImage); NSString *urlString = [ NSString stringWithFormat:@"http://yourUploadImageURl.php?intid=%@",1]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; NSMutableData *body = [NSMutableData data]; [body appenddata:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appenddata:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name="userfile"; filename="%@"rn", 1]] dataUsingEncoding:NSUTF8StringEncoding]]; [body appenddata:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appenddata:[NSData dataWithdata:imageData]]; [body appenddata:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:body]; [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)