在iPhone中创建自定义图库

时间:2013-02-11 04:20:44

标签: ios iphone objective-c uiimagepickercontroller

我使用以下代码从图库中选择照片或直接使用相机

- (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. 想要将已挑选的照片保存在该自定义目录中。
  3. 我是iPhone开发中的新人,所以等待你的宝贵帮助。 提前谢谢。

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:1)

iOS 5为您提供了使用ALAssetsLibrary

添加自定义相册的功能

这是一个教程iOS5: Saving photos in custom photo album


修改

如果链接变为非活动状态

在.h文件中创建ivar

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