2

SamplePhotosApp の次のコードに基づいています。

- (void)applyFilterWithName:(NSString *)filterName {
    PHContentEditingInputRequestOptions *options = [[PHContentEditingInputRequestOptions alloc] init];
    [options setCanHandleAdjustmentData:^BOOL(PHAdjustmentData *adjustmentData) {
        return [adjustmentData.formatIdentifier isEqualToString:AdjustmentFormatIdentifier] && [adjustmentData.formatVersion isEqualToString:@"1.0"];
    }];

    [self.asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
        NSURL *url = [contentEditingInput fullSizeImageURL];
        int orientation = [contentEditingInput fullSizeImageOrientation];
        CIImage *inputImage = [CIImage imageWithContentsOfURL:url options:nil];
        inputImage = [inputImage imageByApplyingOrientation:orientation];

        // Add filter
        CIFilter *filter = [CIFilter filterWithName:filterName];
        [filter setDefaults];
        [filter setValue:inputImage forKey:kCIInputImageKey];
        CIImage *outputImage = [filter outputImage];

        // Create editing output
        NSData *jpegData = [outputImage aapl_jpegRepresentationWithCompressionQuality:0.9f];
        PHAdjustmentData *adjustmentData = [[PHAdjustmentData alloc] initWithFormatIdentifier:AdjustmentFormatIdentifier formatVersion:@"1.0" data:[filterName dataUsingEncoding:NSUTF8StringEncoding]];

        PHContentEditingOutput *contentEditingOutput = [[PHContentEditingOutput alloc] initWithContentEditingInput:contentEditingInput];
        [jpegData writeToURL:[contentEditingOutput renderedContentURL] atomically:YES];
        [contentEditingOutput setAdjustmentData:adjustmentData];

        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:self.asset];
            request.contentEditingOutput = contentEditingOutput;
        } completionHandler:^(BOOL success, NSError *error) {
            if (!success) {
                NSLog(@"Error: %@", error);
            }
        }];
    }];
}

画像にフィルターを適用する独自の実装を作成しました。私のコード:

- (void)saveAsset:(PHAsset *)asset withEdits:(NSDictionary *)edits withCompletion:(successBlock)block {
    PHContentEditingInputRequestOptions *options = [[PHContentEditingInputRequestOptions alloc] init];
    [options setCanHandleAdjustmentData:^BOOL(PHAdjustmentData *adjustmentData) {
        return [adjustmentData.formatIdentifier isEqualToString:@"myID"] && [adjustmentData.formatVersion isEqualToString:@"1.0"];
    }];

    [asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
        NSURL *fullImageURL = [contentEditingInput fullSizeImageURL];
        UIImage *fullSizeImage = [UIImage imageWithContentsOfFile:[fullImageURL path]];
        UIImage *editedImage = [EngineImageFilters applyEffects:edits[@"filters"] toImages:@[fullSizeImage] withValue:@(ImageFilterValueTypeCustom)][0];
        NSData *imageData = UIImageJPEGRepresentation(editedImage, 1.0);
        NSData *editData = [NSJSONSerialization dataWithJSONObject:edits options:0 error:nil];
        PHAdjustmentData *adjustmentData = [[PHAdjustmentData alloc] initWithFormatIdentifier:@"profcam" formatVersion:@"1.0" data:editData];
        PHContentEditingOutput *output = [[PHContentEditingOutput alloc] initWithContentEditingInput:contentEditingInput];
        [imageData writeToURL:[output renderedContentURL] atomically:YES];
        [output setAdjustmentData:adjustmentData];

        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:asset];
            request.contentEditingOutput = output;
        } completionHandler:^(BOOL success, NSError *error) {
            if (block) {
                block(success, error);
            }
        }];
    }];
}

多くの値が nil であるため、私のスニペットはクラッシュを引き起こします。requestContentEditingWithOptions:completionHandler メソッドで PHContentEditingInput に nil が返されるため、ファイルの URL が nil になるなどの問題が発生します。また、setCanHandleAdjustmentData ブロックが呼び出されないようです。おそらく、これが PHContentEditingInput が nil である理由です。とにかく、どうすればこの問題を解決できますか?

4

0 に答える 0