【问题标题】:array of images in document directory文档目录中的图像数组
【发布时间】:2013-07-16 10:00:58
【问题描述】:

我通过 ELC 控制器从 iPhone 中选择了多个图像。图像存储在数组中,现在我想将此图像数组存储在文档目录中,所以请帮帮我..

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]){
    [self dismissViewControllerAnimated:YES completion:nil];
} else {
    [self dismissModalViewControllerAnimated:YES];
}

for (UIView *v in [_scrollView subviews]) {
    [v removeFromSuperview];
}

CGRect workingFrame = _scrollView.frame;
workingFrame.origin.x = 0;

NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];


for(NSDictionary *dict in info) {

    UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
    [images addObject:image];

    UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
    [imageview setContentMode:UIViewContentModeScaleAspectFit];
    imageview.frame = workingFrame;

    [_scrollView addSubview:imageview];
    [imageview release];

    workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
}

self.chosenImages = images;
NSLog(@"values=%@",_chosenImages);

[_scrollView setPagingEnabled:YES];
[_scrollView setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
}

【问题讨论】:

  • 您是如何尝试保存它们的,哪些没有奏效?
  • 先生,我不知道一次保存数组,我一次只添加一个图像
  • 想必你想将每张图片作为单独的文件保存在指定的文件夹中?
  • 是的,你当然明白我的问题......

标签: ios objective-c nsdocumentdirectory


【解决方案1】:
  1. 对于images数组中的每一张图片,逐一写入文件file。

    NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSInteger anIndex = 0;
    for (UIImage *anImage in images) {
       NSString *anImageName = [NSString stringWithFormat:@"%d.png", anIndex++];
       NSString *anImagePath = [NSString stringWithFormat:@"%@/%@", aDocumentsDirectory, anImageName];
    
       NSData *anImageData = UIImagePNGRepresentation(anImage);
      [anImageData writeToFile:anImagePath atomically:YES];
    }
    

2.. 提取原始图像时,将图像保存到文件中。

    NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSInteger anIndex = 0;

    for(NSDictionary *dict in info) {
       UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
       [images addObject:image];

       UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
       [imageview setContentMode:UIViewContentModeScaleAspectFit];
       imageview.frame = workingFrame;

       [_scrollView addSubview:imageview];
       [imageview release];

       workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;

       // Save Image
       NSString *anImageName = [NSString stringWithFormat:@"%d.png", anIndex++];
       NSString *anImagePath = [NSString stringWithFormat:@"%@/%@", aDocumentsDirectory, anImageName];

       NSData *anImageData = UIImagePNGRepresentation(image);
       [anImageData writeToFile:anImagePath atomically:YES];
}

【讨论】:

    猜你喜欢
    • 2012-03-31
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 2012-01-27
    • 2016-05-19
    • 2010-11-04
    • 2023-03-03
    相关资源
    最近更新 更多