Ios 从NSData中的电影获取缩略图

Ios 从NSData中的电影获取缩略图,ios,objective-c,nsdata,Ios,Objective C,Nsdata,我的应用程序允许用户从ELCImagePickerController选择视频,我将该视频存储在NSData中。下面是我用来获取NSData的代码 if ([mediaType isEqualToString:@"ALAssetTypeVideo"]) { ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init]; [assetLibrary assetForURL:[[info objectAtI

我的应用程序允许用户从ELCImagePickerController选择视频,我将该视频存储在NSData中。下面是我用来获取NSData的代码

if ([mediaType isEqualToString:@"ALAssetTypeVideo"]) {

        ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
        [assetLibrary assetForURL:[[info objectAtIndex:x] valueForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {

            ALAssetRepresentation *rep = [asset defaultRepresentation];

            unsigned long DataSize = (unsigned long)[rep size];

            Byte *buffer = (Byte*)malloc(DataSize);
            NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:DataSize error:nil];
            NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];//this is NSData may be what you want

            [self.imagesToUpload addObject:data];
            [self.collectionView reloadData];

        } failureBlock:^(NSError *err) {
            NSLog(@"Error: %@",[err localizedDescription]);
        }];  
}
然后我调用这个函数从电影的NSData中检索缩略图,但是movieURL总是返回nil

- (UIImage *)imageFromMovie:(NSData *)movieData {

    // set up the movie player
    NSString *dataString = [[NSString alloc] initWithData:movieData encoding:NSUTF16StringEncoding];
    NSURL *movieURL = [[NSURL alloc] initWithString:dataString];

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:movieURL options:nil];
    AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    NSError *err = NULL;
    CMTime time = CMTimeMake(1, 60);
    CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
    NSLog(@"err==%@, imageRef==%@", err, imgRef);

    return [[UIImage alloc] initWithCGImage:imgRef];

}

对于包含非法字符(如空格)的URL,NSURL将返回nil。检查是否有。我想您需要使用-[NSString stringByAddingPercentEscapesUsingEncoding:]

 NSString *urlString = [[NSString alloc] initWithData: movieData encoding:NSUTF8StringEncoding];
 NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

您是否试图获取包含电影数据的URL?但是电影数据不是您试图从中提取图像的资源的URL,但资源本身的URL字符串和URL都返回null