本文介绍了将图像上传到Web服务-入门的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须上传通过相机拍摄的图像或通过uiimagepickerview上传的图像.我正在使用ASIHTTPRequest.

I have to upload an image taken via the camera or uploaded via uiimagepickerview. I am using ASIHTTPRequest.

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];

1.)因为我是从iPhone相机拍摄照片或从uiimagepickerview上传照片,所以我需要知道应该为setFile(@"/Users/ben/Desktop/ben.jpg"的替换文字)设置什么值?

1.) Since i am taking the photo from the iPhone camera or uploading it from the uiimagepickerview, i need to know what value should i set for setFile (replacement text for @"/Users/ben/Desktop/ben.jpg") ?

2.)我还需要测试该应用程序,所以没有人知道相应的PHP代码,我可以在其中显示所拍摄的图像(在Web浏览器上).教程或示例代码

2.) I also need to test this application, so does anyone know the corresponding PHP code where i could display the image taken (on the web browser). Tutorials or sample codes

推荐答案

要获得第一个问题的答案,请查看以下代码片段:

To get answer on the 1st question, please, take a look on the following code fragment:

#import <MobileCoreServices/MobileCoreServices.h>

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    BOOL isImage = ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:kUTTypeImage]);
    if (isImage) {
        self.request = [ASIFormDataRequest requestWithURL:url];
        UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
        NSData *imageData = UIImageJPEGRepresentation(originalImage, 0.7);
        [self.request setData:imageData
                 withFileName:@"photo.jpg"
               andContentType:@"image/jpeg"
                       forKey:@"photo"];
        [self.request start];
    }
}

请考虑将属性添加到您的UIImagePiclerController委托类中

Please consider to add property into your UIImagePiclerController delegate class

@property(nonatomic, retain) ASIFormDataRequest *request;

如果您的对象已被释放,它将允许您停止请求或至少将其委托设置为nil,只需执行即可

It will allow you to stop request or at least set it's delegate to nil, if your object is deallocated, by simply implementing

- (void) dealloc {
    [self.request cancel];
    self.request.delegate = nil;
    [request release];
    [super dealloc];
}

这篇关于将图像上传到Web服务-入门的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:08