转:ios的图片文件上传代码

转自: https://gist.github.com/igaiga/1354221

 

@interface ImageUploader : NSObject {
NSData *theImage;
}
@property (retain) NSData *theImage;
- (void) syncUpload:(NSData *) uploadImage;
@end
 
 
#import "ImageUploader.h"
 
#define NOTIFY_AND_LEAVE(X) {[self cleanup:X]; return;}
#define DATA(X) [X dataUsingEncoding:NSUTF8StringEncoding]
 
// Posting constants
#define IMAGE_CONTENT @"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"
#define BOUNDARY @"------------0x0x0x0x0x0x0x0x"
 
@implementation ImageUploader
@synthesize theImage;
 
 
- (void) syncUpload:(NSData *) uploadImage
{
self.theImage = uploadImage;
[self main];
}
 
- (void) cleanup: (NSString *) output
{
self.theImage = nil;
// NSLog(@"******** %@", output);
}
 
- (NSData*)generateFormDataFromPostDictionary:(NSDictionary*)dict
{
id boundary = BOUNDARY;
NSArray* keys = [dict allKeys];
NSMutableData* result = [NSMutableData data];
 
for (int i = 0; i < [keys count]; i++)
{
id value = [dict valueForKey: [keys objectAtIndex:i]];
[result appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 
if ([value isKindOfClass:[NSData class]])
{
// handle image data
NSString *formstring = [NSString stringWithFormat:IMAGE_CONTENT, [keys objectAtIndex:i]];
[result appendData: DATA(formstring)];
[result appendData:value];
}
 
NSString *formstring = @"\r\n";
[result appendData:DATA(formstring)];
}
 
NSString *formstring =[NSString stringWithFormat:@"--%@--\r\n", boundary];
[result appendData:DATA(formstring)];
return result;
}
 
 
// NSOperationQueueによる非同期化を見据えてmainにしています。
- (void) main
{
if (!self.theImage) {
NOTIFY_AND_LEAVE(@"Please set image before uploading.");
}
NSString* MultiPartContentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BOUNDARY];
 
NSMutableDictionary* post_dict = [[NSMutableDictionary alloc] init];
[post_dict setObject:@"testimage" forKey:@"filename"];
[post_dict setObject:self.theImage forKey:@"data[User][image]"];
 
NSData *postData = [self generateFormDataFromPostDictionary:post_dict];
[post_dict release];
 
NSString *baseurl = @"https://example.com/api/upload_image";
NSURL *url = [NSURL URLWithString:baseurl];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
if (!urlRequest) NOTIFY_AND_LEAVE(@"Error creating the URL Request");
 
[urlRequest setHTTPMethod: @"POST"];
[urlRequest setValue:MultiPartContentType forHTTPHeaderField: @"Content-Type"];
[urlRequest setHTTPBody:postData];
 
NSError *error;
NSURLResponse *response;
NSData* result = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
//NSLog(@"***** result =%@", result);
 
if (!result)
{
[self cleanup:[NSString stringWithFormat:@"upload error: %@", [error localizedDescription]]];
return;
}
 
// Return results
NSString *outstring = [[[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"***** outstring =%@", outstring);
[self cleanup: outstring];
}
 
@end
posted @ 2015-12-10 10:59  跬步者  阅读(2792)  评论(0编辑  收藏  举报