2010-12-16 104 views
43

我使用保存合併圖像到iPhone照片庫:iPhone:如何獲取使用UIImageWriteToSavedPhotosAlbum()保存的圖像的文件路徑?

UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil); 

並獲得使用回調:

- (void) savedPhotoImage:(UIImage*)image didFinishSavingWithError:(NSError *)error contextInfo: (void *)contextInfo { NSLog(@"%@", [error localizedDescription]); 
NSLog(@"info: %@", contextInfo);} 

我想得是在圖像具有路徑已保存,所以我可以將它添加到數組中,該數組將用於調用應用程序中其他位置保存的項目列表。

當我使用選取器加載圖像時,它顯示路徑信息。 但是,當我保存一個創建的圖像時,我找不到將保存的圖像路徑拉到哪裏。

我有關於網絡的搜索,但大多數例子停止在回調與一個不錯的消息說圖像保存成功。 我只想知道它的保存位置。

我明白一種方法可能是開始定義我自己的路徑,但由於該方法對我來說,我只是希望它能告訴我它保存在哪裏。

回答

85

我終於找出答案。 顯然,UIImage方法去掉元數據,所以使用UIImageWriteToSavedPhotosAlbum並不好。

但是,在ios4中,Apple放入了一個新的框架來處理稱爲ALAssetsLibrary的照片庫。

首先,您需要右鍵單擊目標,然後在構建部分中,使用左下角的小圖標將AlAsset框架添加到項目中。

然後將#import "AssetsLibrary/AssetsLibrary.h";添加到您班級的頭文件中。

最後,您可以使用下面的代碼:

UIImage *viewImage = YOUR UIIMAGE // --- mine was made from drawing context 
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
// Request to save the image to camera roll 
[library writeImageToSavedPhotosAlbum:[viewImage CGImage] orientation:(ALAssetOrientation)[viewImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){ 
    if (error) { 
     NSLog(@"error"); 
    } else { 
      NSLog(@"url %@", assetURL); 
    } 
}]; 
[library release]; 

這讓你剛剛保存的文件的路徑。

+0

不錯!感謝分享。 – 2013-09-01 20:36:21

+0

如何加載該圖像?我可以加載圖像窗體畫廊?任何人都可以發佈代碼來加載? – 2016-03-02 14:33:53

+7

ALAssetsLibrary在iOS 9.0以上版本中已棄用。而是使用PHPhotoLibrary和「performChanges」方法將圖像保存到設備。 – Tys 2016-06-03 14:31:52

0
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo { 

    RandomIndexnew = arc4random() % 3; 
    if(RandomIndexnew == 0) 
    { 
     nameStr =[NSString stringWithFormat:@"jpg"]; 
     textFieldNormalFile_type.text =[NSString stringWithFormat:@"jpg"]; 
    } 
    else if(RandomIndexnew = 1) 
    { 
     nameStr =[NSString stringWithFormat:@"gif"]; 
     textFieldNormalFile_type.text =[NSString stringWithFormat:@"GIF"]; 
    } 
    else if(RandomIndexnew = 2) 
    { 
     nameStr =[NSString stringWithFormat:@"jpg"]; 
     textFieldNormalFile_type.text =[NSString stringWithFormat:@"JPG"]; 
    } 

    RandomIndex = arc4random() % 20; 
    NSString *nameStr1 =[NSString stringWithFormat:@"Image%i",RandomIndex]; 
    textFieldNormalFile_name.text =[NSString stringWithFormat:@"%@.%@",nameStr1,nameStr]; 

    newFilePath = [NSHomeDirectory() stringByAppendingPathComponent: textFieldNormalFile_name.text]; 
    imageData = UIImageJPEGRepresentation(img, 1.0); 
    if (imageData != nil) { 
     NSLog(@"HERE [%@]", newFilePath); 
     [imageData writeToFile:newFilePath atomically:YES]; 
    } 
    image.image =[UIImage imageNamed:newFilePath]; 
    NSLog(@"newFilePath:%@",newFilePath); 
    path.text =[NSString stringWithFormat:newFilePath]; 
    NSLog(@"path.text :%@",path.text); 
} 
+0

我正在實施這個代碼我自己的應用程序和它的工作..你可以修改你自己的代碼來實現這一點。我希望它能幫助你。 – sandy 2010-12-16 07:57:21

3

我的代碼是

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; 

    imageURL = nil; 

    ALAssetsLibraryWriteImageCompletionBlock completeBlock = ^(NSURL *assetURL, NSError *error){ 
     if (!error) { 
      #pragma mark get image url from camera capture. 
      imageURL = [NSString stringWithFormat:@"%@",assetURL]; 

     } 
    }; 

    if(image){ 
     ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
     [library writeImageToSavedPhotosAlbum:[image CGImage] 
            orientation:(ALAssetOrientation)[image imageOrientation] 
           completionBlock:completeBlock]; 
    } 
} 
在.H導入庫

,並定義ALAssetsLibraryWriteImageCompletionBlock

#import <UIKit/UIKit.h> 
#import <AssetsLibrary/AssetsLibrary.h> 

typedef void (^ALAssetsLibraryWriteImageCompletionBlock)(NSURL *assetURL, NSError *error); 

的類型定義,如果你不知道如何獲得<AssetsLibrary/AssetsLibrary.h>,請添加現有框架(AssetsLibrary.framework)

1

Swift Version將爲

ALAssetsLibrary().writeImageToSavedPhotosAlbum(editedImage.CGImage, orientation: ALAssetOrientation(rawValue: editedImage.imageOrientation.rawValue)!, 
       completionBlock:{ (path:NSURL!, error:NSError!) -> Void in 
        print("\(path)") 
      }) 

和「在您的文件中導入ALAssetsLibrary」。

項目 - >構建階段 - >鏈接二進制 - > AssetsLibrary.framework

+0

你可以更新它的Swift 2.0嗎? – 2016-06-06 18:10:18

0

ALAssetsLibrary已被棄用。

這是你應該做的方式:

#import <Photos/Photos.h> 

UIImage *yourImage; 

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 
    [PHAssetChangeRequest creationRequestForAssetFromImage:yourImage]; 
} completionHandler:^(BOOL success, NSError *error) { 
    if (success) { 
     NSLog(@"Success"); 
    } else { 
     NSLog(@"write error : %@",error); 
    } 
}]; 
相關問題