How to select image either from camera or photo gallery and then upload to server in xcode?


Here is the method for selecting option for image:

-(IBAction)doAddPhoto:(id)sender
{
    UIActionSheet* popupQuery = [[UIActionSheet alloc] initWithTitle:@”” delegate:self cancelButtonTitle:@”Cancel” destructiveButtonTitle:nil otherButtonTitles:@”Take New Picture”, @”Choose From Library”, nil];
    popupQuery.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    [popupQuery showInView:self.view];
    [popupQuery release];
}

Action sheet for selecting image from device.

-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{    
    if(buttonIndex == 1) {
        //[self uploadFileToServer:@”test.png”];
        //return;
        UIImagePickerController * picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        picker.allowsEditing=TRUE;
        [self presentModalViewController:picker animated:YES];
    } else if (buttonIndex == 0) {
        
        
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController * picker = [[[UIImagePickerController alloc] init] autorelease];
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.allowsEditing=TRUE;
            [self presentModalViewController:picker animated:YES];
        }
        else {
            UIAlertView* alertView = [[[UIAlertView alloc] initWithTitle:nil message:@”Can not find Camera Device” delegate:nil cancelButtonTitle:@”OK” otherButtonTitles:nil, nil] autorelease];
            [alertView show];
        }
    }
    
}

Here code for resizing image.

– (UIImage*)imageWithImage:(UIImage*)image
              scaledToSize:(CGSize)newSize
{
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}
// Code for Uploading image to server

-(NSString *)uploadFileToServer:(NSString *)fileName

{

/* creating path to document directory and appending filename with extension */

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
    
    NSData *file1Data = [[NSData alloc] initWithContentsOfFile:filePath];
    
   // NSString *urlString = @”http://www.yourserver.com/applink/fileup.php”;
    NSString *urlString = @”http://www.yourserver.com/applink/fileup.php”;
    
    /* creating URL request to send data */
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    
    [request setURL:[NSURL URLWithString:urlString]];
    
    [request setHTTPMethod:@”POST”];
    
    NSString *boundary = [NSString stringWithString:@”—————————14737809831466499882746641449″];
    
    NSString *contentType = [NSString stringWithFormat:@”multipart/form-data; boundary=%@”,boundary];
    
    [request addValue:contentType forHTTPHeaderField: @”Content-Type”];
    
    /* adding content as a body to post */
    
    NSMutableData *body = [NSMutableData data];
    
    NSString *header = [NSString stringWithFormat:@”Content-Disposition: form-data; name=\”%@\”; filename=\”.%@\”\r\n”,[fileName stringByDeletingPathExtension],[fileName pathExtension]];
    
    [body appendData:[[NSString stringWithFormat:@”\r\n–%@\r\n”,boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    [body appendData:[[NSString stringWithString:header] dataUsingEncoding:NSUTF8StringEncoding]];
    
    [body appendData:[[NSString stringWithString:@”Content-Type: application/octet-stream\r\n\r\n”] dataUsingEncoding:NSUTF8StringEncoding]];
    
    [body appendData:[NSData dataWithData:file1Data]];
    
    [body appendData:[[NSString stringWithFormat:@”\r\n–%@–\r\n”,boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    [request setHTTPBody:body];
    
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] ;
    NSLog(@”return string =%@”,returnString);
    return returnString;
    
}

Advertisements

3 thoughts on “How to select image either from camera or photo gallery and then upload to server in xcode?

  1. I wanna say thanks for publicing this correct information. Keep up this great job. I’ll subscribe to your blog also. thnx!

  2. When it’s all reported and done, KMI derives 95% of its dollars stream from KMP. One time many of the bucks is gathered and taxed, KMI pays a dividend to shareholders. Stakeholders you should not should do anything special listed here, as KMI is a stock much like every other stock, and its dividends are only like every other. That versatility is appealing to some traders.
    http://www.artisanprint.co.uk/policy_files/stacks.asp?p=51 http://www.artisanprint.co.uk/policy_files/stacks.asp?p=51

  3. With steel brackets securely fix and level your aquarium then carefully slide onto the brackets.
    Be warned however, that excess food can create excess nutrients in your
    tank, creating a water quality problem. Lastly,
    chemical filtration helps to keep your aquarium water clean and sparkling.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s