2013-09-29 77 views
3

我是新來解析,並試圖保存和上傳視頻到雲。這是我正在使用的代碼,但在調用doneButtonAction時它會一直出現錯誤。我相信問題出在視頻被保存爲文件的時候,但我不知道如何解決這個問題。謝謝你在前進 -使用Parse.com上傳視頻

- (void)imagePickerController:(UIImagePickerController *)picker  didFinishPickingMediaWithInfo:(NSDictionary *)info { 

    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; 
    NSLog(@"%@", videoURL); 


    if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (self.moviePath)) 
    { 
     UISaveVideoAtPathToSavedPhotosAlbum (self.moviePath, nil, nil, nil); 
    } 

    [self shouldUploadVideo]; 
    [self doneButtonAction]; 
} 

- (void)shouldUploadVideo { 

    //Capturamos el NSUserdefault para grabar el evento 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSString *loadstring = [defaults objectForKey:@"Video"]; 
    NSLog(@"Working at first line"); 



    NSData *videoData = [NSData dataWithContentsOfURL:self.videoURL]; 
    NSLog(@"Working at second line"); 


    PFFile *videoFile = [PFFile fileWithData:videoData]; 
    NSLog(@"Working at third line"); 

    [self.videoFile saveInBackground]; 
    NSLog(@"Working at last line"); 

} 

- (void)doneButtonAction { 
    // Make sure there was no errors creating the image files 
    if (!self.videoFile) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Couldn't save your photo" 
                 message:nil 
                 delegate:nil 
               cancelButtonTitle:nil 
               otherButtonTitles:@"Dismiss",nil]; 
     [alert show]; 
     return; 
    } 

    // Create a Photo object 
    PFObject *video = [PFObject objectWithClassName:kPAPPhotoClassKey]; 
    [video setObject:[PFUser currentUser] forKey:kPAPPhotoUserKey]; 
    [video setObject:self.videoFile forKey:@"Video"]; 

    // Request a background execution task to allow us to finish uploading 
    // the photo even if the app is sent to the background 
    self.photoPostBackgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 
     [[UIApplication sharedApplication] endBackgroundTask:self.photoPostBackgroundTaskId]; 
    }]; 

    // Save the Photo PFObject 
    [video saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
     if (succeeded) { 
      NSLog(@"Photo uploaded"); 

      // Add the photo to the local cache 
      [[PAPCache sharedCache] setAttributesForPhoto:video likers:[NSArray array] commenters:[NSArray array] likedByCurrentUser:NO]; 

      // Send a notification. The main timeline will refresh itself when caught 
      [[NSNotificationCenter defaultCenter] postNotificationName:PAPTabBarControllerDidFinishEditingPhotoNotification object:video]; 
     } else { 
      NSLog(@"Photo failed to save: %@", error); 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Couldn't post your photo" 
                  message:nil 
                  delegate:nil 
                cancelButtonTitle:nil 
                otherButtonTitles:@"Dismiss",nil]; 
      [alert show]; 
     } 

     // If we are currently in the background, suspend the app, otherwise 
     // cancel request for background processing. 
     [[UIApplication sharedApplication] endBackgroundTask:self.photoPostBackgroundTaskId]; 
    }]; 

    // Dismiss this screen 
    [self.parentViewController dismissViewControllerAnimated:YES completion:nil]; 

} 
+0

有什麼錯誤? – rmaddy

+0

因此,在doneBottonAction中,如果文件沒有保存正確,它會發送一條消息,並且這會一直被調用@rmaddy –

+0

我會再次提問。什麼是錯誤?發佈您獲得的完整錯誤消息。 – rmaddy

回答

3

- (void)shouldUploadVideo { 

您有:

PFFile *videoFile = [PFFile fileWithData:videoData]; 
NSLog(@"Working at third line"); 

[self.videoFile saveInBackground]; 
NSLog(@"Working at last line"); 

您要上傳self.videoFile背景 - 不錄像檔案

這意味着videoFile不在後臺上傳PFFile。

變化

PFFile *videoFile = [PFFile fileWithData:videoData]; 

self.videoFile = [PFFile fileWithData:videoData]; 
+0

謝謝sooooo多 –

+1

請注意,有一個10MB的限制 –

+0

有沒有你的代碼的快速版本? –