图像的阵列中的文件目录

时间:2013-07-17 17:49:59

标签: ios objective-c nsdocumentdirectory

我有通过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)];
}

1 个答案:

答案 0 :(得分:1)

  1. 对于images数组中的每个图像,逐个写入文件。

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