2013-02-11 68 views
0

我用下面的代碼來接從畫廊的照片或直接使用相機創建iPhone自定義圖片庫

- (void)actionSheet:(UIActionSheet *)actionSheet 
     clickedButtonAtIndex:(NSInteger)buttonIndex { 

    UIImagePickerController *imagePickerController = 
     [[UIImagePickerController alloc]init]; 

    imagePickerController.delegate = self; 
    if (buttonIndex == 0) { 
     photoButtonNum=0; 
     [self presentViewController:imagePickerController 
          animated:YES 
         completion:nil]; 
     imagePickerController.sourceType = 
      UIImagePickerControllerSourceTypePhotoLibrary; 
    } else if (buttonIndex == 1) { 
     photoButtonNum=1; 
     [self presentViewController:imagePickerController 
          animated:YES 
         completion:nil]; 
     imagePickerController.sourceType = 
      UIImagePickerControllerSourceTypeCamera; 
    } 
} 

我期待創建自己的應用程序保存的拍攝照片的自定義目錄(文件夾)在iPhone中。我需要你給

  1. 幫助創建自己的應用程序的自定義目錄
  2. 要保存在自定義目錄中挑選照片。

我是iPhone開發的新人,所以等待您的寶貴幫助。 在此先感謝。

回答

1

這個網站有助於您創建和保存Photo in your Directory

,你也可以使用下面的代碼。

- (void)imagePickerController:(UIImagePickerController *)picker 
      didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    UIImage *pickedImage = 
     [info objectForKey:UIImagePickerControllerOriginalImage]; 
    NSData *imageData = UIImagePNGRepresentation(pickedImage); 

    NSString *documentsDirectory = 
     [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
              NSUserDomainMask, 
              YES) lastObject]; 
    NSString *path = [documentsDirectory 
     stringByAppendingPathComponent:@"imageName.png"]; 

    NSError * error = nil; 
    [imageData writeToFile:storePath options:NSDataWritingAtomic error:&error]; 

    if (error != nil) { 
     NSLog(@"Error: %@", error); 
     return; 
    } 
} 
+0

非常感謝創建伊娃,我會盡力.. – 2013-02-11 04:36:01

+0

這裏'storePath'是什麼?它本身就是'路徑'嗎? – 2013-02-11 05:19:34

+0

它是你想要存儲圖像的路徑... – iPatel 2013-02-11 05:21:45

1

的iOS 5給你的功能使用ALAssetsLibrary

這裏是一個教程iOS5: Saving photos in custom photo album


編輯

添加自定義相冊如果鏈接變爲無效

在.h文件中

ALAssetsLibrary* library; 

然後在你的代碼很可能在viewDidLoad

library = [[ALAssetsLibrary alloc] init]; 

然後在您的委託方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo 
{ 
    [self.library saveImage:image toAlbum:@"Touch Code Magazine" withCompletionBlock:^(NSError *error) { 
     if (error!=nil) { 
      NSLog(@"Big error: %@", [error description]); 
     } 
    }]; 

    [picker dismissModalViewControllerAnimated:NO]; 
} 
+1

@SamBudda它是好的,雖然我編輯了我的答案,以防鏈接變爲無效。 SO中不建議給出答案中的鏈接。每當我遇到只包含鏈接的回答(這些回覆都是我在SO的最初職業生涯中發佈的)時,我都會編輯它們 – 2014-03-07 15:45:16