在iOS中保存照片的原始文件名

时间:2012-10-02 07:50:46

标签: iphone objective-c ios xcode

以下是我在图片选择器中保存图片的方式,我的图片选择器使用defaultRepresentation,有点AlAsset

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask ,YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%u.png", i]];

ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];

//----resize the images
image = [self imageByScalingAndCroppingForSize:image toSize:CGSizeMake(256,256*image.size.height/image.size.width)];

//----fix image orientation
image  = [image fixSlotOrientation];

NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:YES];

我的图像选择器以这种方式工作:当按下按钮,相机滚动弹出,您选择所需的相册,然后您可以选择图像(多个或单个),然后按下完成后,它将保存到{ {1}}。在最初,当按下按钮时,您仍然可以看到勾选标记,但是当应用程序关闭时它会消失。所以我想通过原来的路径保存它。

如何通过从原始路径获取图像来保存我的图像,然后将选中的图像(多个/单个)存储在NSCachesDirectory中,稍后(应用程序从初始关闭后)我可以用来重新加载图像再次出现。

1 个答案:

答案 0 :(得分:0)

最初在NSUserDefault中添加一个NSMutableArray。

 [[NSUserDefault standardDefault] setObject:[NSMutableArray array] forKey:@"PickedImage"];
 [[NSUserDefault standardDefault] synchronize];

因此你从didFinishPickingMediaWithInfo获取imageURL:

NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
if([[NSUserDefault standardDefault] objectForKey:@"PickedImage"])
{
   NSMutableArray arrImages = [[NSUserDefault standardDefault] objectForKey:@"PickedImage"];
   [arrImages addObject:url];

   [[NSUserDefault standardDefault] setObject:arrImages forKey:@"PickedImage"];
 [[NSUserDefault standardDefault] synchronize]; 
}