Swift中的PHFetchResult错误

时间:2014-11-15 13:26:27

标签: objective-c image swift ios8

我翻译了一个项目的代码,该代码完全可以从swift中的obj-c开始。它从用户直接从cameraRoll导入的一组图像中创建一部电影。如果我在用obj-c编写的项目中运行代码工作正常,但如果我遇到swift会给我以下错误:

  

2014-11-15 13:07:38.535 CEMovieMaker [1278:406246] - [PHFetchResult CGImage]:无法识别的选择器发送到实例0x7f88f1fa20a0

     

2014-11-15 13:07:38.536 CEMovieMaker [1278:406246] ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [PHFetchResult CGImage]:无法识别的选择器发送到实例0x7f88f1fa20a0'

***首先抛出调用堆栈:

(
     0   CoreFoundation                      0x0000000103891f35 __exceptionPreprocess + 165
     1   libobjc.A.dylib                     0x0000000102df5bb7 objc_exception_throw + 45
     2   CoreFoundation                      0x000000010389904d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
     3   CoreFoundation                      0x00000001037f127c ___forwarding___ + 988
     4   CoreFoundation                      0x00000001037f0e18 _CF_forwarding_prep_0 + 120
     5   CEMovieMaker                        0x000000010278e655 __53-[CEMovieMaker createMovieFromImages:withCompletion:]_block_invoke + 277
     6   AVFoundation                        0x000000010346db8e -[AVAssetWriterInputMediaDataRequester requestMediaDataIfNecessary] + 88
     7   libdispatch.dylib                   0x00000001074c3ba6 _dispatch_call_block_and_release + 12
     8   libdispatch.dylib                   0x00000001074e17f4 _dispatch_client_callout + 8
     9   libdispatch.dylib                   0x00000001074c9b22 _dispatch_queue_drain + 1417
     10  libdispatch.dylib                   0x00000001074c9432 _dispatch_queue_invoke + 235
     11  libdispatch.dylib                   0x00000001074cbfc1 _dispatch_root_queue_drain + 685
     12  libdispatch.dylib                   0x00000001074cd5d9 _dispatch_worker_thread3 + 111
     13  libsystem_pthread.dylib             0x00000001078656cb _pthread_wqthread + 729
     14  libsystem_pthread.dylib             0x00000001078634a1 start_wqthread + 13
)

libc ++ abi.dylib:以NSException类型的未捕获异常终止

(lldb)

这里是代码:

var photoAsset:PHFetchResult!

@IBAction func creaFilmato(sender: AnyObject) {
        println("Crea il tuo filmato")
        let img1:UIImage = UIImage(named: "image1.jpg")!
        let settings:NSDictionary = CEMovieMaker.videoSettingsWithCodec(AVVideoCodecH264, withWidth: img1.size.width, andHeight: img1.size.height)
        let prova = CEMovieMaker(settings: settings)
        prova.createMovieFromImages([self.photoAsset.copy()], withCompletion: {(success:Bool, fileURL:NSURL!) in
            if(success){
                self.viewMovieAtURL(fileURL)
                // Save movie in cameraRoll
                let library:ALAssetsLibrary = ALAssetsLibrary()
                library.writeVideoAtPathToSavedPhotosAlbum(fileURL, completionBlock: {(assetURL:NSURL!, error) in
                    if((error) != nil) {
                       println("Errore nel salvataggio del filmato %@",error)
                    }
                })
            }
        })
        println("Esportato!")
    }
    func viewMovieAtURL(fileURL:NSURL!)->Void {
        let playerController:MPMoviePlayerViewController = MPMoviePlayerViewController(contentURL: fileURL!)
        playerController.view.frame = self.view.bounds
        self.presentMoviePlayerViewControllerAnimated(playerController)
        playerController.moviePlayer.prepareToPlay()
        playerController.moviePlayer.play()
        self.view.addSubview(playerController.view)
    }

有人可以帮助我吗?

我试图给出命令self.photoAsset.copy(),但结果总是一样的。我把这段代码放在obj -c

- (IBAction)process:(id)sender {

    NSMutableArray *frames = [[NSMutableArray alloc] init];
    //NSMutableArray *frames;

    UIImage *img1 = [UIImage imageNamed:@"image1"];
    UIImage *img2 = [UIImage imageNamed:@"image2"];
    UIImage *img3 = [UIImage imageNamed:@"image3"];
    UIImage *img4 = [UIImage imageNamed:@"image4"];
    UIImage *img5 = [UIImage imageNamed:@"image5"];

    //frames = [[NSMutableArray alloc] initWithObjects:img1, img2, img3, img4, img5, nil];

    NSDictionary *settings = [CEMovieMaker videoSettingsWithCodec:AVVideoCodecH264 withWidth:img1.size.width andHeight:img1.size.height];
    self.movieMaker = [[CEMovieMaker alloc] initWithSettings:settings];

    for (NSInteger i = 0; i < 2; i++) {

        [frames addObject:img1];
        [frames addObject:img2];
        [frames addObject:img3];
        [frames addObject:img4];
        [frames addObject:img5];
    }

    [self.movieMaker createMovieFromImages:[frames copy] withCompletion:^(BOOL success, NSURL *fileURL){

        if (success) {

            [self viewMovieAtUrl:fileURL];

            //scrivere qui il codice per salvare il video in camera roll??
            ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
            [library writeVideoAtPathToSavedPhotosAlbum:fileURL
                                        completionBlock:^(NSURL *assetURL, NSError *error) {
                                            if (error)
                                                NSLog(@"Error saving to camera roll: %@",error);
                                            //else
                                            // remove temporary movie file
                                        }];
        }
    }];
}


- (void)viewMovieAtUrl:(NSURL *)fileURL
{
    MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
    [playerController.view setFrame:self.view.bounds];
    [self presentMoviePlayerViewControllerAnimated:playerController];
    [playerController.moviePlayer prepareToPlay];
    [playerController.moviePlayer play];
    [self.view addSubview:playerController.view];
}

此处函数为swift,因为我传递了一组图像。我希望而不是数组由应用程序创建的文件夹中的所有图像组成

1 个答案:

答案 0 :(得分:1)

我怀疑你的错误就在这里:

    prova.createMovieFromImages([self.photoAsset.copy()], withCompletion: {(success:Bool, fileURL:NSURL!) in
                                ^^^^^^^^^^^^^^^^^^^^^^^^

您传递的数组包含一个PHFectchResult项,而不是createMoveFromImages期待的项目。

您需要将从照片中提取的UIImages放入[NSObject]类型的数组中,以便传入createMovieFromImages