上传视频时iOS 4.3崩溃

时间:2012-02-21 19:34:24

标签: objective-c ios ios5 ios4

我有一个iPhone应用程序,使用ASIHTTPRequest将图像和视频上传到Web服务。一切都在运行iOS 5的设备上运行良好,但在从UIImagePickerController返回后在4.3及以下设备上崩溃。视频在选择器上被压缩,然后应用程序崩溃。下面是 - (void)imagePickerController:didFinishPickingMediaWithInfo:方法的代码,以及将视频发布到服务器的方法以及从视频中捕获图像以用作缩略图的方法。关于导致4.3崩溃的原因的任何想法?

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

self.mediaType = [info objectForKey:UIImagePickerControllerMediaType];

 if ([self.mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

    // Get asset to use for orientation determination   
    AVAsset *myAsset = [AVAsset assetWithURL:videoURL];

    self.videoOrientation = [self UIInterfaceOrientationToString:[self orientationForTrack:myAsset]];

    UIImage *videoThumbnail = [self getVideoThumbnailFromAVAsset:myAsset];
    self.photoImageView.image = videoThumbnail;

    self.videoData = [[NSMutableData alloc]initWithContentsOfURL:videoURL];
}
[self dismissModalViewControllerAnimated:YES]; 
}

-(UIImage*)getVideoThumbnailFromAVAsset:(AVAsset *)myAsset {
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:myAsset];

Float64 durationSeconds = CMTimeGetSeconds([myAsset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
NSError *error = nil;
CMTime actualTime;
UIImage *myImage;

CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];

if (halfWayImage != NULL) {
    myImage = [UIImage imageWithCGImage:halfWayImage];
    CGImageRelease(halfWayImage);
}
return myImage;
}

- (void)postMessage:(id)sender {

self.uploadButton.enabled = NO;
[self.descriptionTextField resignFirstResponder];
[self.titleTextField resignFirstResponder];

NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
NSString *sessionKey = [settings stringForKey: @"sessionKey"];

// build URL for request
NSString *baseURL;
NSString *endPoint;

// code here that creates the url component strings baseURL and endPoint

NSString *fullURL = [NSString stringWithFormat:@"%@%@", baseURL, endPoint];
NSURL *url = [NSURL URLWithString:fullURL];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
request.postFormat = ASIMultipartFormDataPostFormat;

NSString *teamIDStr = [NSString stringWithFormat:@"%d",[self.teamID intValue]];

[request setPostValue:sessionKey forKey:@"sessionKey"];
[request setPostValue:teamIDStr forKey:@"TeamID"];   
[request setPostValue:titleTextField.text forKey:@"lessonName"];
[request setPostValue:descriptionTextField.text forKey:@"lessonDesc"];
[request setPostValue:self.videoOrientation forKey:@"videoOrientation"];

//create video file name
NSDate *date1=[NSDate date];
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat:@"hhmm"];
NSString *valuestr = [formatter1 stringFromDate:date1];
NSString *moviename = [NSString stringWithFormat:@"video_%@.mov", valuestr];

[request setData:self.videoData withFileName:moviename andContentType:@"video/quicktime" forKey:@"file1.mov"];

[request setUploadProgressDelegate:self.progressBar];
[request setShowAccurateProgress:YES];
self.progressBar.hidden = NO;
[request setDelegate:self];
[request setTimeOutSeconds:600];
[request startAsynchronous];

}

1 个答案:

答案 0 :(得分:4)

这个问题的简单回答:AVAsset类方法

+ (id)assetWithURL:(NSURL *)URL
iOS 5.0之前

不可用。您必须使用具体的子类AVURLAsset来实例化iOS 4.0和4.3的AVAsset,使用其类方法

+ (AVURLAsset *)URLAssetWithURL:(NSURL *)URL options:(NSDictionary *)options