使用iOS在Objective-C中使用SOAP在base64中发送图像

时间:2015-07-20 05:42:12

标签: ios objective-c soap uiimage

我是iOS开发的新手。我试图通过使用SOAP将其编码为base64格式来发送单击的图像。我不知道该怎么做。

这是我的imagePickerController委托:

  // delegate method for picking images
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  {
     NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];

    if([mediaType isEqualToString:(NSString*)kUTTypeImage])
   {
    UIImage *photoTaken = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    //Save Photo to library only if it wasnt already saved i.e. its just been taken
     if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) 
      {
        UIImageWriteToSavedPhotosAlbum(photoTaken, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

     NSData *data=[[NSData alloc] initWithData:UIImagePNGRepresentation(photoTaken)];
        base64= [[NSString alloc]init];
        base64 =[data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];    
      }
    }

//   [picker dismissModalViewControllerAnimated:YES];

    [picker dismissViewControllerAnimated:YES completion:NULL];
    [picker release];
}

3 个答案:

答案 0 :(得分:2)

在didFinishPickingMediaWithInfo ..

 UIImage* chosenImage =info[UIImagePickerControllerEditedImage];

//encoding image to base64

    imgData=[[NSData alloc] initWithData:UIImagePNGRepresentation(chosenImage)];
_base64=[[NSString alloc]init];

    _base64=[imgData  base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
self.tempbase= _base64;

并在soap消息中调用tempbase

答案 1 :(得分:0)

图像尺寸会很大。它可以降低您的应用性能,因此首先通过调整图像大小来缩小图像大小。

-(UIImage *) imageWithImage:(UIImage *) image scaledTOSize:(CGSize) newsize
{
    UIGraphicsBeginImageContext(newsize);
    [image drawInRect:CGRectMake(0, 0, newsize.width, newsize.height)];
    UIImage *newImg=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImg;
}

现在将这个小号UIImage转换为NSData

 NSData *imgData=[[NSData alloc] initWithData:UIImagePNGRepresentation(image)];

然后使用第三方库将<{1}}转换为base64字符串 -

  

base64.h

     

的NSData + Base64.h

NSData

现在将此字符串发送到您的服务。

答案 2 :(得分:0)

代码:

UIGraphicsBeginImageContext(self.drawImage.frame.size);
[self.drawImage.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *imageView = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData* data = UIImageJPEGRepresentation(imageView, 1.0f);
[Base64 initialize];
NSString *strEncoded = [Base64 encode:data];

注意:drawImage是UIImageView和Import Base64.h类的对象。