0

这是我在图像选择器中保存图像的方法,我的图像选择器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];

我的图像选择器是这样工作的:当按下按钮时,相机胶卷弹出,你选择你想要的相册,然后你可以选择图像(多张或单张)然后当按下完成时,它将保存到NSCachesDirectory. 最初,当按下按钮时,您仍然可以看到复选标记,但是当应用程序关闭时它就消失了。所以我正在考虑通过其原始路径保存它。

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

4

1 回答 1

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]; 
}
于 2012-10-02T08:37:03.717 回答