Domanda

I have many file types in my app and I have to let the user open the files while staying in the app. For example, I have PDF files and photo from the photo album. I read that UIDocumentInteractionController can open whatever file type I want. My worry is about file path: can I open also images of the photo album whose path is like: "assets-library://"?

I tried this code:

- (void)setupDocumentControllerWithPath:(NSString *)path
{

    if (path == nil || [path length] == 0) return;
    NSURL* url = [NSURL fileURLWithPath:path];
    if (self.docInteractionController == nil)
    {
        self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];

        self.docInteractionController.delegate = self;
    }
    else
    {
        self.docInteractionController.URL = url;
    }

    if ([path hasPrefix:@"assets-library"])
    {
        self.docInteractionController.UTI = @"jpeg";
        CGRect rect = CGRectMake(x, y,w,h);


if (![self.docInteractionController presentOpenInMenuFromRect:rect inView:self.view animated:YES])
            NSLog(@"Failed to open file  in Asset");
    }
    else
    {
        if (![self.docInteractionController presentPreviewAnimated:YES])
              NSLog(@"Failed to open document in Document Dir");;
    }
    url = nil;
}

and I call it whenever I want to open a file at a specific path. For asset files, the code enter the Log "Failed to open file in Asset".

È stato utile?

Soluzione

copy the image from photo album to local sandbox first, and then use UIDocumentInteractionController to open the copy in sandbox.

my code:

#define PATH_IMAGE [NSTemporaryDirectory() stringByAppendingPathComponent: @"image"]

- (void)someFunction{
   [self copyPhotoFromAlbumToSandboxAndShare: path completion:^(NSString *url) {
        UIDocumentInteractionController *documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL: [NSURL fileURLWithPath: url]];
        documentInteractionController.delegate = self;
        [documentInteractionController presentOpenInMenuFromRect: CGRectZero inView: self.view animated: YES];
    }]; //path is a "assets-library" schema url string
}



- (void)copyPhotoFromAlbumToSandboxAndShare:(NSString *)path completion:(void (^)(NSString *))completion {
    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary assetForURL: [NSURL URLWithString: path] resultBlock:^(ALAsset *asset) {
    BOOL isExist = NO;
    if (nil != asset) {
        NSString *fileName = [asset defaultRepresentation].filename;
        NSString *path = [PATH_IMAGE stringByAppendingPathComponent: fileName];
        if ([[NSFileManager defaultManager] fileExistsAtPath: path]) {
            NSDictionary* imageAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath: path error:nil];
            NSDate *imageCreationDate = [imageAttributes objectForKey: NSFileCreationDate];
            if ([imageCreationDate isEqualToDate: [asset valueForProperty: ALAssetPropertyDate]]) {
                isExist = YES;
                completion(path);
            }
        }

        if (!isExist) {
            UIImage *image = [UIImage imageWithCGImage: [asset defaultRepresentation].fullResolutionImage];
            [self saveImage: image path: path creationDate: [asset valueForProperty: ALAssetPropertyDate]];
            completion(path);
        }
    }
} failureBlock:^(NSError *error) {

}];
}

- (void)saveImage:(UIImage *)image path:(NSString *)path creationDate:(NSDate *)date{
    if (nil == image || nil == path) {
        return;
    }

    NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
    if (nil == imageData) {
        imageData = UIImagePNGRepresentation(image);
    }
    if (![[NSFileManager defaultManager] fileExistsAtPath: [path stringByDeletingLastPathComponent] isDirectory: NULL]) {
        [[NSFileManager defaultManager] createDirectoryAtPath: [path stringByDeletingLastPathComponent] withIntermediateDirectories: YES attributes: nil error: nil];
}
NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary];
[attributesDictionary setValue: date forKey: NSFileCreationDate];
[[NSFileManager defaultManager] createFileAtPath: path contents: imageData attributes: attributesDictionary];
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top