我的应用程序中有很多文件类型,我必须让用户在应用程序中打开文件。
例如,我有相册中的PDF文件和照片。
我读到UIDocumentInteractionController
可以打开我想要的任何文件类型。
我担心的是文件路径:我是否可以打开相册的路径如下图像:
"assets-library://"
?
我试过这段代码:
- (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;
}
每当我想在特定路径打开文件时,我都会调用它。 对于资产文件,代码输入日志“无法在资产中打开文件”。
答案 0 :(得分:2)
首先将图像从相册复制到本地沙箱,然后使用UIDocumentInteractionController在沙箱中打开副本。
我的代码:
#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];
}