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];
}