我是iPhone App Development的新手。在我的代码中,我能够从库中选择多个图像并获得仅1个图像的路径。然后我在Documents目录中的一个名为“images”的文件夹中创建该图像的副本并尝试压缩它。现在我想获得所有选择的多个图像的路径,将它们复制到文件目录中的同一文件夹“images”中,我想稍后压缩它们。 请告诉我如何在我的代码中完成上述任务。 这就是我的代码现在的样子:
- (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);
}
答案 0 :(得分: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目录中保存总是比在文档目录中更好,请检查is saving in NSDocumentDirectory okay?
但是如果你想将它保存在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"];