如何在iOS 10中导出Apple新的RAW照片格式.dng?

时间:2016-08-24 07:17:41

标签: ios uiactivityviewcontroller ios10 photokit

我无法导出Apple新的RAW照片格式.dng。

UIActivityViewController将照片导出为JPEG,这违背了目的。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

以下是我解决问题的方法。

-(void)activityAction{
PHAsset *asset = self.assetsFetchResults[(int)self.image_number];

PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous = NO;
options.version = PHImageRequestOptionsVersionCurrent;
options.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic;
options.resizeMode = PHImageRequestOptionsResizeModeNone;
options.networkAccessAllowed = NO;//HAS TO BE YES TO DOWNLOAD FROM THE CLOUD AND PROGRESS TO SHOW
/*options.progressHandler =  ^(double progress,NSError *error,BOOL* stop, NSDictionary* dict) {
    NSLog(@"progress %lf",progress);  //never gets called
};*/

[[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation imageOrientation, NSDictionary *info){

    NSURL *assetURL = [info objectForKey:@"PHImageFileURLKey"];
    NSString *photoFormat = [assetURL pathExtension];

    NSURL *temporaryDNGFileURL;

    NSArray *activityItems;

    if([photoFormat isEqualToString:@"DNG"] && [[UIDevice currentDevice].systemVersion floatValue] >= 10){
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        [format setDateFormat:@"MMddyyyyHHmmss"];
        NSString *assetDateString = [format stringFromDate:[NSDate date]];
        NSString *assetPath = [documentsDirectory stringByAppendingPathComponent:[assetDateString stringByAppendingString:@".dng"]];
        temporaryDNGFileURL = [NSURL fileURLWithPath:assetPath];
        [imageData writeToURL:temporaryDNGFileURL atomically:YES];

        activityItems = @[temporaryDNGFileURL];
    }
    else{
        UIImage *shareImage = [[UIImage alloc] initWithData:imageData];

        activityItems = @[shareImage];
    }

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    activityVC.excludedActivityTypes = @[UIActivityTypeSaveToCameraRoll];
    activityVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:activityVC animated:YES completion:nil];

    [activityVC setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
        NSString *ServiceMsg = nil;
        if( [activityType isEqualToString:UIActivityTypeMail] )           ServiceMsg = @"Mail Done";
        if( [activityType isEqualToString:UIActivityTypeMessage] ) ServiceMsg = @"Message Done";
        if( [activityType isEqualToString:UIActivityTypeAirDrop] ) ServiceMsg = @"AirDrop Done";
        if( [activityType isEqualToString:UIActivityTypePostToTwitter] )  ServiceMsg = @"Twitter Done";
        if( [activityType isEqualToString:UIActivityTypePostToFacebook] ) ServiceMsg = @"Facebook Done";
        if( [activityType isEqualToString:UIActivityTypePostToFlickr] ) ServiceMsg = @"Flickr Done";
        if( [activityType isEqualToString:UIActivityTypePostToTencentWeibo] ) ServiceMsg = @"Weibo Done";

        if(completed){
            [customActivityActionPressedTimer invalidate];
            customActivityActionPressedTimer = nil;
            customActivityAction = 0;
            [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"customActivityAction"];

            NSLog(@"ACTIVITY VIEW DONE");

            if([[NSFileManager defaultManager] fileExistsAtPath:temporaryDNGFileURL.path]){
                NSError *error;
                [[NSFileManager defaultManager] removeItemAtPath:temporaryDNGFileURL.path error:&error];
                if(error){
                    NSLog(@"RAW DNG FILE DELETE FAILED");
                }
                else{
                    NSLog(@"RAW DNG FILE DELETED");
                }
            }
        }
        else{
            [customActivityActionPressedTimer invalidate];
            customActivityActionPressedTimer = nil;
            customActivityAction = 0;
            [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"customActivityAction"];

            NSLog(@"ACTIVITY VIEW CANCEL");

            if([[NSFileManager defaultManager] fileExistsAtPath:temporaryDNGFileURL.path]){
                NSError *error;
                [[NSFileManager defaultManager] removeItemAtPath:temporaryDNGFileURL.path error:&error];
                if(error){
                    NSLog(@"RAW DNG FILE DELETE FAILED");
                }
                else{
                    NSLog(@"RAW DNG FILE DELETED");
                }
            }
        }
    }];
}];

}