@brunofuster

www.wesaveapp.com

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:

UploadViewController.h


@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;

@end

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.

UploadViewController.m


#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];
}

@end

Security issues will be discussed in a new post.

References:
http://www.zimbio.com/iPhone/articles…
http://developer.apple.com/library/…UIImagePickerController.html
http://iphone.zcentric.com/…/using-a-uiimagepickercontroller/
http://iphone.zcentric.com/…/post-a-uiimage-to-the-web/
http://allseeing-i.com/ASIHTTPRequest/S3
http://stackoverflow.com/questions/1754184/amazon-s3-post-upload-from-iphone
http://docs.amazonwebservices.com/AmazonS3/2006-03-01/dev/index.html?UsingHTTPPOST.html
http://stackoverflow.com/questions/1053944/uploading-to-amazon-s3-services-from-iphone-app
http://developer.apple.com/library/ios/…UIImage_Class
http://blog.objectgraph.com/…/download-an-image…
http://developer.apple.com/library/mac/…NSData_Class

Advertisements

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