Created
October 1, 2014 02:30
-
-
Save squishykid/de07038dd89fb7a050f4 to your computer and use it in GitHub Desktop.
AWS Upload View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RWUploadView.h | |
// helpers/gists | |
// | |
// Created by Robin Wohlers-Reichel on 24/08/2014. | |
// Copyright (c) 2014 robinwohlers. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@class RWUploadView; | |
@protocol RWUploadViewDelegate <NSObject> | |
- (void) rwUploadViewDidFinish:(RWUploadView *)controller; | |
@end | |
@interface RWUploadView : UIView { | |
UILabel *statusLabel; | |
UIActivityIndicatorView *activityIndicator; | |
} | |
@property (weak, nonatomic) id <RWUploadViewDelegate> delegate; | |
- (void)beginMovieUploadForURL:(NSURL *)url withName:(NSString *)name; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RWUploadView.m | |
// helpers/gists | |
// | |
// Created by Robin Wohlers-Reichel on 24/08/2014. | |
// Copyright (c) 2014 robinwohlers. All rights reserved. | |
// | |
#import "RWUploadView.h" | |
#import <AVFoundation/AVFoundation.h> | |
#import <Parse/Parse.h> | |
#import <AWSiOSSDKv2/S3.h> | |
#import "Constants.h" | |
@implementation RWUploadView | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.origin.x + 36.0f, self.frame.origin.y + 14.5f, 276.0f, 21.0f)]; | |
statusLabel.text = @"Finishing Video"; | |
[self addSubview:statusLabel]; | |
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; | |
activityIndicator.frame = CGRectMake(8.0f, 15.0f, 20.0f, 20.0f); | |
[activityIndicator startAnimating]; | |
[self addSubview:activityIndicator]; | |
[self setBackgroundColor:[UIColor colorWithWhite:0.9 alpha:1]]; | |
} | |
return self; | |
} | |
-(void)beginMovieUploadForURL:(NSURL *)url withName:(NSString *)name{ | |
[self handleMediaUploadForURL:url withName:name]; | |
} | |
-(void)initialiseMediaUploadForURL:(NSURL *)url withName:(NSString *)name { | |
NSString *commonFileName = [self getRandomFilename]; | |
AVURLAsset *asset = [AVURLAsset assetWithURL:url]; | |
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; | |
[imageGenerator generateCGImagesAsynchronouslyForTimes:@[[NSValue valueWithCMTime:kCMTimeZero]] completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) { | |
if (image != nil) { | |
[self uploadImageForUIImage:[UIImage imageWithCGImage:image] withFileName:commonFileName]; | |
} | |
}]; | |
} | |
-(void) handleMediaUploadForURL:(NSURL *)url withName:(NSString *)name { | |
//start loading indicator | |
//generate random file name | |
//upload video | |
//generate thumbnail | |
//upload thumbnail | |
//add to database | |
//end loading indicator | |
NSLog(@"started handling"); | |
UIApplication* app = [UIApplication sharedApplication]; | |
app.networkActivityIndicatorVisible = YES; | |
//generate file name | |
NSString *commonFileName = [self getRandomFilename]; | |
//upload video | |
[self uploadVideoForURL:url withFileName:commonFileName]; | |
//generate thumbnail | |
AVURLAsset *asset = [AVURLAsset assetWithURL:url]; | |
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; | |
[imageGenerator generateCGImagesAsynchronouslyForTimes:@[[NSValue valueWithCMTime:kCMTimeZero]] completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) { | |
if (image != nil) { | |
[self uploadImageForUIImage:[UIImage imageWithCGImage:image] withFileName:commonFileName]; | |
} | |
}]; | |
//add to database | |
PFObject *videoEntry = [PFObject objectWithClassName:@"videos"]; | |
videoEntry[@"videoTitle"] = name; | |
videoEntry[@"videoUser"] = commonFileName; | |
videoEntry[@"videoURL"] = [NSString stringWithFormat:@"%@.mp4",commonFileName]; | |
videoEntry[@"thumbnailURL"] = [NSString stringWithFormat:@"%@.jpg",commonFileName]; | |
videoEntry[@"videoLikeCount"] = @0; | |
[videoEntry saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { | |
[self.delegate rwUploadViewDidFinish:self]; | |
}]; | |
//end loading indicator | |
app.networkActivityIndicatorVisible = NO; | |
} | |
- (void) uploadImageForUIImage:(UIImage *)image withFileName:(NSString *)fileName { | |
//save to file | |
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES]; | |
NSURL *imageURL = [[tmpDirURL URLByAppendingPathComponent:fileName] URLByAppendingPathExtension:@"jpg"]; | |
[UIImageJPEGRepresentation(image, 0.7f) writeToURL:imageURL atomically:YES]; | |
//upload | |
AWSS3TransferManagerUploadRequest *imageUpload = [AWSS3TransferManagerUploadRequest new]; | |
imageUpload.bucket = S3BucketName; | |
imageUpload.key = [NSString stringWithFormat:@"%@.jpg",fileName]; | |
imageUpload.body = imageURL; | |
imageUpload.contentType = @"image/jpeg"; | |
NSLog(@"image file name:%@",fileName); | |
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager]; | |
[[transferManager upload:imageUpload] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) { | |
//delete image on disk on completion | |
[[NSFileManager defaultManager] removeItemAtURL:imageURL error:nil]; | |
NSLog(@"image upload error %@",task.error); | |
return nil; | |
}]; | |
} | |
- (void) uploadVideoForURL:(NSURL *)url withFileName:(NSString *)fileName { | |
AWSS3TransferManagerUploadRequest *videoObject = [AWSS3TransferManagerUploadRequest new]; | |
videoObject.bucket = S3BucketName; | |
videoObject.key = [NSString stringWithFormat:@"%@.mp4",fileName]; | |
videoObject.body = url; | |
videoObject.contentType = @"video/mp4"; | |
NSLog(@"video file name:%@",fileName); | |
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager]; | |
[[transferManager upload:videoObject] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) { | |
//delete video file on disk on completion | |
[[NSFileManager defaultManager] removeItemAtURL:url error:nil]; | |
NSLog(@"video upload error %@",task.error); | |
return nil; | |
}]; | |
} | |
- (NSString *)getRandomFilename{ | |
NSString *alphabet = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789"; | |
NSMutableString *s = [NSMutableString stringWithCapacity:20]; | |
for (NSUInteger i = 0U; i < 20; i++) { | |
u_int32_t r = arc4random() % [alphabet length]; | |
unichar c = [alphabet characterAtIndex:r]; | |
[s appendFormat:@"%C", c]; | |
} | |
return [NSString stringWithString:s]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment