iOS 4.3 上传视频时崩溃

发布于 2025-01-07 22:49:18 字数 3403 浏览 0 评论 0原文

我有一个 iPhone 应用程序,它使用 ASIHTTPRequest 将图像和视频上传到 Web 服务。在运行 iOS 5 的设备上一切正常,但在 4.3 及更低版本的设备上从 UIImagePickerController 返回后会崩溃。视频通过选择器进行压缩,然后应用程序崩溃。下面是来自 -(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];

}

I have an iPhone app that uploads images and video to a web service using ASIHTTPRequest. Everything works great on devices running iOS 5, but crashes on 4.3 and below devices after returning from the UIImagePickerController. The video is compressed bu=y the picker, then the app crashes. Below is the code from the -(void)imagePickerController: didFinishPickingMediaWithInfo: method, and also from the method that post the video to the server and a method that captures an image from the video to use as a thumbnail. Any ideas on what's causing the crashes in 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];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请恋爱 2025-01-14 22:49:18

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

+ (id)assetWithURL:(NSURL *)URL

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

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

SImple answer to this issue: The AVAsset class method

+ (id)assetWithURL:(NSURL *)URL

is not available before iOS 5.0. You must use the concrete subclass AVURLAsset to instantiate an AVAsset for iOS 4.0 and 4.3, using its class method

+ (AVURLAsset *)URLAssetWithURL:(NSURL *)URL options:(NSDictionary *)options
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文