Skip to content

Instantly share code, notes, and snippets.

@simonpang
Created September 20, 2012 06:22
Show Gist options
  • Save simonpang/3754248 to your computer and use it in GitHub Desktop.
Save simonpang/3754248 to your computer and use it in GitHub Desktop.
Upload photo to server
#pragma mark DLCImagePickerControllerDelegate
-(void) imagePickerControllerDidCancel:(DLCImagePickerController *)picker{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
[self dismissModalViewControllerAnimated:YES];
}
- (void)uploadMediaInfo:(NSDictionary *)info {
UIImage *resizedImage = [[info objectForKey:@"image"] imageCroppedToFitSize:CGSizeMake(800, 800)];
NSData *jpegData = UIImageJPEGRepresentation(resizedImage, 0.5);
NSString *key = self.randomKey;
NSString *tmpFile = [NSString pathWithComponents:@[self.temporaryDirectory, key]];
[jpegData writeToFile:tmpFile atomically:NO];
dispatch_async(dispatch_get_main_queue(), ^{
// Upload to S3
NSLog(@"Uploading to S3.....");
AFAmazonS3Client *s3Client = [[AFAmazonS3Client alloc] initWithAccessKeyID:kAccessKeyId secret:kSecretAccessKey] ;
NSString *destPath = [NSString stringWithFormat:@"http://XXXXX.s3-ap-southeast-1.amazonaws.com/"];
s3Client.bucket = @"campfire-pilot";
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:key, @"key", nil];
[s3Client postObjectWithFile:tmpFile destinationPath:destPath parameters:params progress:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
} success:^(id responseObject) {
[s3Client release];
// Upload photo
NSLog(@"Creating Photo .....");
HttpClient *httpClient = [[HttpClient alloc] init];
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
@"1", @"photo[user_id]",
@"1", @"photo[event_id]",
@"hello", @"photo[caption]",
key, @"photo[url]", nil];
[httpClient postPath:@"photos.json" parameters:d
success:^(AFHTTPRequestOperation *operation, id responseObject) {
[httpClient showMessage:@"Photo Created!"];
[httpClient release];
[self dismissModalViewControllerAnimated:YES];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[httpClient showMessage:[error description]];
[httpClient release];
}];
} failure:^(NSError *error) {
[_httpClient showMessage:[error description]];
[s3Client release];
}];
});
}
-(void) imagePickerController:(DLCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
if (info) {
[self performSelectorInBackground:@selector(uploadMediaInfo:) withObject:info];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment