Ext: iOS image File upload code

Source: Internet
Author: User

Transferred from: 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による of non-contemporaneous えて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

Ext: iOS image File upload code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.