iOS 4.3 上传视频时崩溃
Posted
技术标签:
【中文标题】iOS 4.3 上传视频时崩溃【英文标题】:iOS 4.3 crashes when uploading video 【发布时间】:2012-02-21 19:34:23 【问题描述】:我有一个使用 ASIHTTPRequest 将图像和视频上传到网络服务的 iPhone 应用程序。在运行 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];
【问题讨论】:
它在模拟器中崩溃了吗,你能找到它崩溃的地方吗?那么你可能能够回答这个问题:) 我无法弄清楚如何将视频添加到模拟器。当我将它们放在保存的照片存储图像的目录中时,它们不会出现在 UIImagePickerVirewControllerNSLog(@"Documents: %@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]);
也许?
我可以看到获取视频方向的代码吗?以及如何修复缩略图?
【参考方案1】:
这个问题的简单答案:AVAsset 类方法
+ (id)assetWithURL:(NSURL *)URL
在 iOS 5.0 之前不可用。您必须使用具体的子类 AVURLAsset 来实例化 iOS 4.0 和 4.3 的 AVAsset,使用它的类方法
+ (AVURLAsset *)URLAssetWithURL:(NSURL *)URL options:(NSDictionary *)options
【讨论】:
以上是关于iOS 4.3 上传视频时崩溃的主要内容,如果未能解决你的问题,请参考以下文章