2014-01-17 3 views
1

내 앱에 파일 형식이 많아서 앱에 머무르는 동안 사용자가 파일을 열도록해야합니다. 예를 들어 사진 앨범의 PDF 파일과 사진이 있습니다. UIDocumentInteractionController은 내가 원하는 파일 형식을 열 수 있음을 읽었습니다. 내 경로가 파일 경로입니다. 경로가 다음과 같은 사진 앨범의 이미지도 열 수 있습니까? "assets-library://"? 갤러리에서 UIDocumentInteractionController를 사용하여 이미지 열기

나는이 코드를 시도 :

- (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

을 시도 했습니까? 어떻게 된 거예요? – rmaddy

+0

나는 노력했다. 문서 폴더의 파일에는 문제가 없습니다. 자산에있는 파일의 경우 아무 일도 일어나지 않습니다 ... – giuseppe

+0

아무도 추측을 제안 할 수 없습니까? – giuseppe

답변

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]; 
} 
관련 문제