代码突然停止工作 - 数组没有填充

时间:2015-09-03 08:27:02

标签: ios objective-c photosframework

此代码在2周前工作正常......突然它停止工作,我无法弄清楚原因。

这些是在viewDidLoad中调用的相关方法:

在viewDidLoad中调用:

if(!self.currentProject.assets){
        [self fetchAssets];
        [self fetchImagesFromAssets:nil];
    }else {
        NSArray *assets = [NSKeyedUnarchiver unarchiveObjectWithData:self.currentProject.assets];
        [self fetchImagesFromAssets:assets.mutableCopy];
    }

方法:

-(void) fetchAssets{
    self.assets = [[PHFetchResult alloc]init];
    NSLog(@"Album Name:%@",self.albumName);


    if (self.albumName.length > 0) {


        PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[self.albumName]    options:nil];
        PHAssetCollection *collection = userAlbums[0];

        if (!_isProUser) {

            PHFetchOptions *onlyImagesOptions = [PHFetchOptions new];
            onlyImagesOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType = %i", PHAssetMediaTypeImage];

            NSLog(@"Collection:%@", collection.localIdentifier);

            self.assets = [PHAsset fetchAssetsInAssetCollection:collection options:onlyImagesOptions];

        }
        else
        {
            self.assets = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
        }

    }else{

        PHFetchOptions *options = [PHFetchOptions new];
        options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

        if(!_isProUser){
            options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %i", PHAssetMediaTypeImage];
        }

        self.assets = [PHAsset fetchAssetsWithOptions:options];
    }
}

-(void) fetchImagesFromAssets:(NSMutableArray*)assets{

    if (assets == nil) {
        assets = [[NSMutableArray alloc]init];
        for (int i = (int)self.firstPhotoIndex; i < (int)self.lastPhotoIndex; i++) {
            [assets addObject:[self.assets objectAtIndex:i]];
        }
    }
    self.photosToVideofy = [[NSMutableArray alloc]init];
    NSLog(@"Assets:%lu",(unsigned long)assets.count);

    for (PHAsset *asset in assets)
    {
        if (!_isProUser) {
            [asset requestContentEditingInputWithOptions:nil
                                       completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
                                           NSURL *imageURL = contentEditingInput.fullSizeImageURL;
                                           NSLog(@"ImageURL:%@", imageURL);
                                           [self.photosToVideofy addObject:imageURL];
                                       }];


        }else{


            if (asset.mediaType == PHAssetMediaTypeVideo) {

                [self generateImagesFromAsset:asset];


            }else if (asset.mediaType == PHAssetMediaTypeImage){

                [asset requestContentEditingInputWithOptions:nil
                                           completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
                                               NSURL *imageURL = contentEditingInput.fullSizeImageURL;
                                               [self.photosToVideofy addObject:imageURL];
                                           NSLog(@"ImageURL:%@", imageURL);
                                           }];
            }
        }
    }

    assets = nil;

    NSLog(@"There are %lu photos to Videofy", (unsigned long)self.photosToVideofy.count);
}

-(void) generateImagesFromAsset:(PHAsset *)phAsset{
    NSLog(@"Generating images from video...");
    [[PHImageManager defaultManager]requestAVAssetForVideo:phAsset options:nil resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {

        AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
        generator.requestedTimeToleranceAfter =  kCMTimeZero;
        generator.requestedTimeToleranceBefore =  kCMTimeZero;
        generator.maximumSize = self.size;
        int j = 1;
        for (Float64 i = 0; i < CMTimeGetSeconds(asset.duration) *  _FPS ; i++){
            @autoreleasepool {
                CMTime time = CMTimeMake(i, _FPS);
                NSError *err;
                CMTime actualTime;
                CGImageRef image = [generator copyCGImageAtTime:time actualTime:&actualTime error:&err];
                UIImage *generatedImage = [[UIImage alloc] initWithCGImage:image];
                [self saveImage: generatedImage withIndex:self.generatedPhotoIndex]; // Saves the image on document directory and not memory
                CGImageRelease(image);
            }
            j++;
            self.generatedPhotoIndex++;
        }
    }];
}

-(void) saveImage:(UIImage*)image withIndex:(int)index{
    NSData *pngData = UIImagePNGRepresentation(image);
    NSString *path = [self documentsPathForFileName:[NSString stringWithFormat:@"image%i.png",index]];
    NSURL *url = [NSURL URLWithString:path];
    [pngData writeToFile:path atomically:YES]; //Write the file
    NSLog(@"URL:%@",url);
    [self.photosToVideofy addObject:url];

}

-(NSString *)documentsPathForFileName:(NSString *)name{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];

    return [documentsPath stringByAppendingPathComponent:name];
}

我将问题缩小到photosToVideofy数组。它不会填充URL,因为它应该是2周前。

这是控制台的读数:

2015-09-03 11:14:44.899 Videofy[885:110958] First:6 Last:175 BPM:120
2015-09-03 11:14:48.891 Videofy[885:110958] Album Name:(null)
2015-09-03 11:14:51.406 Videofy[885:110958] Assets:169
2015-09-03 11:14:51.436 Videofy[885:110958] Generating images from video...
2015-09-03 11:14:51.460 Videofy[885:110958] Generating images from video...
2015-09-03 11:14:51.461 Videofy[885:110958] Generating images from video...
2015-09-03 11:14:51.465 Videofy[885:110958] Generating images from video...
2015-09-03 11:14:51.472 Videofy[885:110958] Generating images from video...
2015-09-03 11:14:51.474 Videofy[885:110958] There are 0 photos to Videofy

关于为什么会发生这种情况以及如何解决问题的任何想法? (在iPhone 4s和iPad4上测试)

0 个答案:

没有答案