Skip to content

Commit

Permalink
Fixed camera uploading problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Dongsheng Cai committed Jul 26, 2011
1 parent 4696689 commit 1a0478d
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 42 deletions.
9 changes: 2 additions & 7 deletions Moodle.xcodeproj/PreviewViewController.m
Expand Up @@ -22,16 +22,13 @@ -(NSString *)getFilepath {
- (void)loadView
{
[super loadView];
self.title = NSLocalizedString(@"Preview", "Preview");
self.title = NSLocalizedString(@"Preview", nil);
imageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.frame = CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, self.view.bounds.size.height - TTToolbarHeight() - self.navigationController.navigationBar.frame.size.height);
[self.view addSubview: imageView];


self.view.backgroundColor = UIColorFromRGB(ColorBackground);

UIBarButtonItem *buttonUpload = [[UIBarButtonItem alloc] initWithTitle:@"Send to Moodle" style:UIBarButtonItemStylePlain target:self action:@selector(uploadPressed:)];
UIBarButtonItem *buttonUpload = [[UIBarButtonItem alloc] initWithTitle: @"Send to Moodle" style:UIBarButtonItemStylePlain target:self action:@selector(uploadPressed:)];

UIBarItem* space = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
Expand Down Expand Up @@ -126,12 +123,10 @@ - (void)uploadCallback: (id)data {
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//there is only one action sheet on this view, so we can check the buttonIndex against the cancel button
if (buttonIndex == [alertView cancelButtonIndex]) {
NSLog(@"Do nothing, delete file");
// delete file
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:filePath error:nil];
} else {
NSLog(@"Put file in queen");
NSManagedObjectContext *managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"%@", [managedObjectContext hasChanges]);
if (![managedObjectContext save: nil]) {
Expand Down
8 changes: 7 additions & 1 deletion Moodle/AppDelegate.m
Expand Up @@ -122,7 +122,13 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
}
}


if(![NSFm fileExistsAtPath: VIDEO_FOLDER isDirectory:&isDir]) {
if (![NSFm createDirectoryAtPath: VIDEO_FOLDER withIntermediateDirectories:YES attributes:nil error:nil]) {
NSLog(@"Error: Create folder failed");
} else {
NSLog(@"Folder created");
}
}
//Set a method to be called when a notification is sent.
Reachability *reachability = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[reachability startNotifier];
Expand Down
1 change: 1 addition & 0 deletions Moodle/Constants.h
Expand Up @@ -10,6 +10,7 @@
#define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
#define AUDIO_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Audio"]
#define PHOTO_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Photo"]
#define VIDEO_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Video"]

#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
Expand Down
8 changes: 4 additions & 4 deletions Moodle/SettingsSiteViewController.m
Expand Up @@ -346,15 +346,15 @@ - (void)viewDidLoad {
[super viewDidLoad];

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(@"cancel", "cancel button label")
initWithTitle:NSLocalizedString(@"cancel", nil)
style:UIBarButtonItemStylePlain
target:self
action:@selector(cancel:)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];

UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithTitle: NSLocalizedString(@"save", "Save")
initWithTitle: NSLocalizedString(@"save", nil)
style:UIBarButtonItemStyleDone
target:self
action:@selector(saveButtonPressed:)];
Expand All @@ -363,11 +363,11 @@ - (void)viewDidLoad {

if (!newEntry) {
// case of Updating a site
self.title = NSLocalizedString(@"updatesite", @"Update the site");
self.title = NSLocalizedString(@"updatesite", nil);

} else {
//case of Adding a new site
self.title = NSLocalizedString(@"addasite", @"add a site");
self.title = NSLocalizedString(@"addsite", nil);
}
}

Expand Down
3 changes: 2 additions & 1 deletion Moodle/UploadViewController.h
Expand Up @@ -9,8 +9,9 @@
#import <UIKit/UIKit.h>
#import <Three20/Three20.h>
#import "MBProgressHUD.h"
#import "MoodleMedia.h"

@interface UploadViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate, MBProgressHUDDelegate> {
@interface UploadViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate, MBProgressHUDDelegate, MoodleUploadDelegate> {
NSString *fileName;
NSString *filePath;
MBProgressHUD *HUD;
Expand Down
67 changes: 38 additions & 29 deletions Moodle/UploadViewController.m
Expand Up @@ -11,11 +11,18 @@
#import "PreviewViewController.h"
#import "RecorderViewController.h"
#import "MoodleImagePickerController.h"
#import "MoodleMedia.h"
#import <TargetConditionals.h>

@implementation UploadViewController

-(NSString *)getFilepath {
return filePath;
}
- (void)uploadCallback: (id)data {
// [self.navigationController popViewControllerAnimated:YES];
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Complete.png"]] autorelease];
HUD.mode = MBProgressHUDModeCustomView;
HUD.labelText = @"Completed";
}
//this is an Apple function to detect if AAC is enabled
//Source: http://developer.apple.com/library/ios/#qa/qa1663/_index.html
Boolean IsAACHardwareEncoderAvailable(void)
Expand Down Expand Up @@ -84,7 +91,7 @@ - (void)loadView
[uploadIcon release];

UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(130, 35, 190, 55)];
[title setText:@"Upload"];
[title setText: NSLocalizedString(@"Upload", nil)];
[title setFont: [UIFont fontWithName:@"SoulPapa" size:40]];
[title setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:title];
Expand All @@ -94,7 +101,7 @@ - (void)loadView
int y = 90;
int width = 290;
int height = 90;
TTButton *tbutton = [TTButton buttonWithStyle:@"fatButton:" title: NSLocalizedString(@"Browse photo albums", "Browse photo albums")];
TTButton *tbutton = [TTButton buttonWithStyle:@"fatButton:" title: NSLocalizedString(@"browsephotoalbums", "Browse photo albums")];
[tbutton setImage:@"bundle://upload_photo_album.png" forState:UIControlStateNormal];
[tbutton addTarget:self
action:@selector(loadGallery:)
Expand All @@ -104,7 +111,7 @@ - (void)loadView
[self.view addSubview:tbutton];


tbutton = [TTButton buttonWithStyle:@"fatButton:" title: NSLocalizedString(@"Take a picture or video", "Take a picture or video")];
tbutton = [TTButton buttonWithStyle:@"fatButton:" title: NSLocalizedString(@"takepicture", "Take a picture or video")];
[tbutton setImage:@"bundle://upload_camera.png" forState:UIControlStateNormal];

[tbutton addTarget:self
Expand All @@ -115,7 +122,7 @@ - (void)loadView
[self.view addSubview:tbutton];

if (IsAACHardwareEncoderAvailable()) {
tbutton = [TTButton buttonWithStyle:@"fatButton:" title: NSLocalizedString(@"Record audio", "Record audio")];
tbutton = [TTButton buttonWithStyle:@"fatButton:" title: NSLocalizedString(@"recordaudio", "Record audio")];
[tbutton setImage:@"bundle://upload_audio.png" forState:UIControlStateNormal];

[tbutton addTarget:self
Expand Down Expand Up @@ -144,7 +151,7 @@ - (void)viewDidUnload
-(void)loadPreview: (NSString *)filepath withFilename: (NSString *)filename {
PreviewViewController *previewViewController = [[PreviewViewController alloc] init];
//set the dashboard back button just before to push the settings view
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString(@"upload", "upload") style: UIBarButtonItemStyleBordered target: nil action: nil];
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString(@"upload", nil) style: UIBarButtonItemStyleBordered target: nil action: nil];
[[self navigationItem] setBackBarButtonItem: newBackButton];
[newBackButton release];
previewViewController.fileName = filename;
Expand All @@ -158,30 +165,31 @@ - (void)imagePickerController:(UIImagePickerController *)picker didFinishPicking
NSString *strtimestamp = [now description];
//NSURL *mediaUrl;
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
NSLog(@"%@", info);
if ([mediaType isEqualToString: @"public.image"]) {
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
fileName = [NSString stringWithFormat:@"%@.jpg", strtimestamp];
filePath = [NSString stringWithFormat:@"%@/%@", DOCUMENTS_FOLDER, fileName];
filePath = [NSString stringWithFormat:@"%@/%@", PHOTO_FOLDER, fileName];
[UIImageJPEGRepresentation(image, 1.0f) writeToFile: filePath atomically:YES];
if ([info objectForKey:@"UIImagePickerControllerMediaMetadata"]) {
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
// Picked from camera, saving to photo album
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
[picker dismissModalViewControllerAnimated:YES];
// then upload
[self uploadAction];
} else {
[picker dismissModalViewControllerAnimated:YES];
[self loadPreview: filePath withFilename: fileName];
}
} else if ([mediaType isEqualToString:@"public.movie"]) {
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSString *filename = [NSString stringWithFormat:@"%@.mov", strtimestamp];
NSData *data = [NSData dataWithContentsOfURL:videoURL];
NSString *filepath = [NSString stringWithFormat:@"%@/%@", DOCUMENTS_FOLDER, filename];
NSString *filepath = [NSString stringWithFormat:@"%@/%@", VIDEO_FOLDER, filename];
[data writeToFile:filepath atomically:YES];
[picker dismissModalViewControllerAnimated:YES];
// upload now!
[self uploadAction];
}
[picker dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
Expand All @@ -190,21 +198,22 @@ - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;

// Unable to save the image
if (error)
alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
else // All is well
alert = [[UIAlertView alloc] initWithTitle:@"Success"
message:@"Image saved to Photo Album."
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
// UIAlertView *alert;
//
// // Unable to save the image
// if (error) {
// alert = [[UIAlertView alloc] initWithTitle:@"Error"
// message:@"Unable to save image to Photo Album."
// delegate:self cancelButtonTitle:@"Ok"
// otherButtonTitles:nil];
// } else { // All is well
// alert = [[UIAlertView alloc] initWithTitle:@"Success"
// message:@"Image saved to Photo Album."
// delegate:self cancelButtonTitle:@"Ok"
// otherButtonTitles:nil];
// }
// [alert show];
// [alert release];
}

- (void)loadGallery:(id)sender {
Expand Down Expand Up @@ -245,14 +254,14 @@ - (void)loadRecorder:(id)sender {
}

- (void)uploadAction {

// The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
[self.view.window addSubview:HUD];

// Regiser for HUD callbacks so we can remove it from the window at the right time
HUD.delegate = self;
// Show the HUD while the provided method executes in a new thread
[HUD showWhileExecuting:@selector(upload:) onTarget:[MoodleMedia class] withObject:nil animated:YES];
[HUD showWhileExecuting:@selector(upload:) onTarget:[MoodleMedia class] withObject: self animated:YES];
}

#pragma mark -
Expand Down
Binary file modified Moodle/en.lproj/Localizable.strings
Binary file not shown.

0 comments on commit 1a0478d

Please sign in to comment.