Iphone 将uiimage发送到服务器

Iphone 将uiimage发送到服务器,iphone,objective-c,Iphone,Objective C,到目前为止,我使用以下方法将字符串发送到服务器: NSString *reqURL = [NSString stringWithFormat:]; reqURL = [reqURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:reqURL]]; NSURLR

到目前为止,我使用以下方法将
字符串
发送到服务器:

NSString *reqURL = [NSString stringWithFormat:];
reqURL = [reqURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:reqURL]];
NSURLResponse *resp = nil;
NSError *err = nil;
[NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];

我想知道是否有可能将
UIImage
发送到服务器。

如果您的图像是
png
,则使用
UIImagePNGRepresentation
获取
NSData
中的数据

NSData *data = UIImagePNGRepresentation(myUIImage);
NSData *data = UIImageJPEGRepresentation(myUIImage);
如果图像是
jpg
jpeg
,则使用
uiimagejpegresentation
NSData
中获取其数据

NSData *data = UIImagePNGRepresentation(myUIImage);
NSData *data = UIImageJPEGRepresentation(myUIImage);
登记入住

检查下面的SO post,以便将
NSData
UIImage
发送到服务器


如果您的图像是
png
,则使用
UIImagePNGRepresentation
NSData
中获取其数据

NSData *data = UIImagePNGRepresentation(myUIImage);
NSData *data = UIImageJPEGRepresentation(myUIImage);
如果图像是
jpg
jpeg
,则使用
uiimagejpegresentation
NSData
中获取其数据

NSData *data = UIImagePNGRepresentation(myUIImage);
NSData *data = UIImageJPEGRepresentation(myUIImage);
登记入住

检查下面的SO post,以便将
NSData
UIImage
发送到服务器

您可以尝试以下方法:-

    NSData *imagedata=[NSData dataWithData:UIImagePNGRepresentation(self.editedImage)]; 
    NSString *base64string=[imagedata base64EncodedString];
    NSString *str = [NSString stringWithFormat:@"%@/uploadBlogData.php",appUrl];
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:base64string forKey:@"imagedata"];
    [request setRequestMethod:@"POST"];
    [request setDelegate:self];
    [request startSynchronous];

        NSLog(@"responseStatusCode %i",[request responseStatusCode]);
    NSLog(@"responseStatusString %@",[request responseString]);
这段代码到底做了什么,我已经将我的图像转换成NSData,然后再次用Base64字符串编码。 您也可以这样做。

您可以尝试以下方法:-

    NSData *imagedata=[NSData dataWithData:UIImagePNGRepresentation(self.editedImage)]; 
    NSString *base64string=[imagedata base64EncodedString];
    NSString *str = [NSString stringWithFormat:@"%@/uploadBlogData.php",appUrl];
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:base64string forKey:@"imagedata"];
    [request setRequestMethod:@"POST"];
    [request setDelegate:self];
    [request startSynchronous];

        NSLog(@"responseStatusCode %i",[request responseStatusCode]);
    NSLog(@"responseStatusString %@",[request responseString]);
这段代码到底做了什么,我已经将我的图像转换成NSData,然后再次用Base64字符串编码。 您也可以这样做。

使用

这样做:

    -(void)uploadImage{
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

        // Upload a file on disk
        [request setFile:@"/Users/ben/Desktop/ben.jpg" withFileName:@"myphoto.jpg" andContentType:@"image/jpeg"
    forKey:@"photo"];

       // Upload an NSData instance
      [request setData:UIImageJPEGRepresentation(myUIImage) withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];

      [request setDelegate:self];
      [request setDidFinishSelector:@selector(uploadRequestFinished:)];
      [request setDidFailSelector:@selector(uploadRequestFailed:)];

      [request startAsynchronous];
}
使用

这样做:

    -(void)uploadImage{
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

        // Upload a file on disk
        [request setFile:@"/Users/ben/Desktop/ben.jpg" withFileName:@"myphoto.jpg" andContentType:@"image/jpeg"
    forKey:@"photo"];

       // Upload an NSData instance
      [request setData:UIImageJPEGRepresentation(myUIImage) withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];

      [request setDelegate:self];
      [request setDidFinishSelector:@selector(uploadRequestFinished:)];
      [request setDidFailSelector:@selector(uploadRequestFailed:)];

      [request startAsynchronous];
}