본문 바로가기
모바일개발(Mobile Dev)/IOS개발(ObjectC)

Sending Image from Xcode to Server

by 테크한스 2015. 12. 11.
반응형

awesome really 

written by http://www.geronimobile.com/sending-image-from-xcode-to-server/



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 
    //Convert your UIImage to NSDate
    NSData *imageData = UIImageJPEGRepresentation(yourImage,.3);    
 
    if (imageData != nil)
    {
        NSString *filenames = [NSString stringWithFormat:@"SelfieName"];      //set name here
 
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];
 
        NSString *boundary = @"---------------------------14737809831466499882746641449";
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
 
        NSMutableData *body = [NSMutableData data];
 
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 
//I use a method called randomStringWithLength: to create a unique name for the file, so when all the aapps are sending files to the server, none of them will have the same name:
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n",[self randomStringWithLength:10]] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 
        [request setHTTPBody:body];
 
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
 
        //I chose to run my code async so the app could continue doing stuff while the image was sent to the server.
        [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
        {
 
            NSData *returnData = data;
            NSLog(@"data recieved!");
 
            //Do what you want with your return data.
 
        }];
1
2
3
4
5
6
7
8
9
10
11
12
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 
-(NSString *) randomStringWithLength: (int) len {
  
 NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
  
 for (int i=0; i<len; i++) {
 [randomString appendFormat: @"%C", [letters characterAtIndex: arc4random_uniform([letters length]) % [letters length]]];
 }
  
 return randomString;
}

This code  can be implemented almost anywhere in your existing code. Change the url name to your script and you’re almost there! Speaking of scripts…

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
 
$target_path "imageDump/";  //where you want the uploaded images to go, I recommend an upload only directory
$target_path $target_path basename$_FILES['userfile']['name'] );
 
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) {
 
//successful move
else {
 
//unsuccessful move
}
?>

 

And there you go! Hoopefully you found this usefull.

-Jonathan Malott


반응형