1

I am new in iPhone App Development. In my code, i am able to select multiple images from the library and get the path of only 1 image. Then i am creating a copy of that image in Documents directory in a folder called "images" and trying to zip it. Now i want to get the path of all those selected multiple images, copy them in documents directory in the same folder "images" and i want to zip them later. Please tell me how i can do the above mentioned tasks in my code. This is how my code looks as of now:

- (void) imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   if (imagePickerController.allowsMultipleSelection) {
    NSArray *mediaInfoArray = (NSArray *)info;
    NSLog(@"Selected %d photos", mediaInfoArray.count);
    NSData *webData = UIImagePNGRepresentation([[mediaInfoArray objectAtIndex:0] objectForKey:@"UIImagePickerControllerOriginalImage"]);
   NSLog(@"web data length is: %u",[webData length]);
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *localFilePath = [documentsDirectory stringByAppendingString:@"/images.png"];
   [webData writeToFile:localFilePath atomically:YES];

   NSLog(@"localFilePath.%@",localFilePath);
}
4

1 回答 1

1

我使用了相同的 QBImagePickerController 并使用下面的这种方法在 NSMutableArray 中添加了多个图像。请检查,一旦它们被添加到阵列中,您可以在任何需要的地方添加它们。

- (void)imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingMediaWithInfo:(id)info
    {
        if(imagePickerController.allowsMultipleSelection)
        {
            NSArray *mediaInfoArray = (NSArray *)info;        
            for (int i =0; i<mediaInfoArray.count ; i++)
            {
                NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/Folder/Selected Files"];
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSData *data = UIImageJPEGRepresentation([[mediaInfoArray valueForKey:@"UIImagePickerControllerOriginalImage"] objectAtIndex:i], 1.0);
                    NSString *fileName = [stringPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Image%d.png",i]];
                    [data writeToFile:fileName atomically:YES];
                });
                [ImagesArray addObject:[[mediaInfoArray valueForKey:@"UIImagePickerControllerOriginalImage"] objectAtIndex:i]];
            }
        }
        else
        {
            //NSDictionary *mediaInfo = (NSDictionary *)info;
            //NSLog(@"Selected: %@", mediaInfo);
        }
        }       
        [TableView reloadData];        
        [self dismissViewControllerAnimated:YES completion:NULL];
    }

希望这可以帮助 ...

编辑 :

好吧,我给了您一个更好的解决方案,因为保存在 Cache 目录中总是比保存在文档目录中好,请检查是否保存在 NSDocumentDirectory 中好吗?

但是如果你想把它保存在 Documents 目录中,那么你可以简单地替换

NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/Folder/Selected Files"];

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
于 2013-08-20T05:29:15.450 回答