且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在iphone中将图像发布到服务器

更新时间:2023-02-02 17:32:38

给予它回答2时间。
如何将图像转换为二进制iOS中的格式?

Giving the Same Answer 2 Time. How to convert image into binary format in iOS?

您可以使用 CoreGraphics '方法 UIImagePNGRepresentation (UIImage * image),返回 NSData 并保存。如果你想再转换成 UIImage 使用 [UIimage imageWithData:(NSData * data)] 方法创建它。

You can use the CoreGraphics' method UIImagePNGRepresentation(UIImage *image), which returns NSData and save it. and if you want to convert it into again UIImage create it using [UIimage imageWithData:(NSData *data)] method.

- (void)sendImageToServer {
       UIImage *yourImage= [UIImage imageNamed:@"image.png"];
       NSData *imageData = UIImagePNGRepresentation(yourImage);
       NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]];

       // Init the URLRequest
       NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
       [request setHTTPMethod:@"POST"];
       [request setURL:[NSURL URLWithString:[NSString stringWithString:@"http://yoururl.domain"]]];
       [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
       [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
       [request setHTTPBody:imageData];

       NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
       if (connection) {
          // response data of the request
       }
       [request release];
 }