Archive for the ‘ec2/s3’ Category

uploading an image from iphone to Amazon S3 with UIImagePicker and ASIHTTP

with one comment

Just organizing and spreading some ideas and tools ;)

Let’s start creating an UIViewController subclass that will attend to UIImagePickerControllerDelegate protocol, so your iphone user can choose an image to upload:


@interface UploadViewController : UIViewController <UIImagePickerControllerDelegate> {

    IBOutlet UIImageView *selectedImage;
    IBOutlet UIButton *selectImageButton;
    IBOutlet UIButton *uploadImageButton;
    UIImagePickerController *imagePicker;

@property (nonatomic, retain) IBOutlet UIImageView *selectedImage;
@property (nonatomic, retain) IBOutlet UIButton *selectImageButton;
@property (nonatomic, retain) IBOutlet UIButton *uploadImageButton;
@property (nonatomic, retain) UIImagePickerController *imagePicker;

- (IBAction) showImagePicker:(id) sender;
- (IBAction) uploadImage:(id) sender;
- (void) uploadToAS3Job;

#pragma mark UIImagePickerControllerDelegate methods

- (void) imagePickerController:(UIImagePickerController *) picker
        didFinishPickingImage:(UIImage *) image
        editingInfo:(NSDictionary *) editingInfo;

- (void) imagePickerControllerDidCancel:(UIImagePickerController *) picker;


This view has an ImageView and two buttons: selectImage and uploadImage. You have to create and connect them through IB (except imagePicker) or create them programatically, its your call.

There are also two IBAction methods (showImagePicker and uploadImage). You have to connect their respective buttons using “touch down” event.

Now lets code the implementation:
Ps: Import ASIHTTPRequest into your project.


#import "UploadViewController.h"
#import "ASIS3Request.h"

@implementation UploadViewController

@synthesize selectedImage;
@synthesize selectImageButton;
@synthesize uploadImageButton;
@synthesize imagePicker;

//create UIImagePickerController when this view has loaded

-(void) viewDidLoad {

    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.allowsImageEditing = YES;
    self.imagePicker.delegate = self;
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

//show a modal view with the image picker 

-(IBAction) showImagePicker {

    [self presentModalViewController:self.imagePicker animated:YES];

#pragma mark UIImagePickerControllerDelegate
//callback method when an image has been selected 

- (void) imagePickerController:(UIImagePickerController *) picker
        didFinishPickingImage:(UIImage *) image
        editingInfo:(NSDictionary *) editingInfo {

    selectedImage.image = image;
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];

//callback method when the user cancels the selection

- (void) imagePickerControllerDidCancel:(UIImagePickerController *) picker {

    [[picker parentViewController] dismissModalViewControllerAnimated:YES];

//upload image action, will run a task in background 

- (IBAction) uploadImage:(id) sender {

    if (self.selectedImage != nil) {
        [self performSelectorInBackground:@selector(uploadToAS3Job) withObject:nil];

//upload in background (just a sample, you should use NSOperationQueue)

- (void) uploadToAS3Job {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [ASIS3Request setSharedSecretAccessKey:@"my-secret-access-key"];
    [ASIS3Request setSharedAccessKey:@"my-access-key"];
    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(self.selectedImage)];

    ASIS3ObjectRequest *request = 
            [ASIS3ObjectRequest PUTRequestForData:imageData withBucket:@"bucket" key:@"images/path"];
    [request setShouldCompressRequestBody:YES]; //gzip compression
    [request startSynchronous];

    if ([request error]) {
       NSLog(@"%@",[[request error] localizedDescription]);

    [pool release];


- (void) dealloc {

    [imagePicker release];
    [selectedImage release];
    [selectImageButton release];
    [uploadImageButton release];
    [super dealloc];


Security issues will be discussed in a new post.



Written by brunofuster

November 3, 2010 at 3:04 pm

to read list

leave a comment »

just hope to finish them all ;)

update: the appengine and scala book are great, not that i’ve finished yet…

Written by brunofuster

August 19, 2010 at 7:01 pm

Posted in cloud, ec2/s3, gae, hadoop, mysql, scala