2012-10-02 40 views
0

以下是我如何在圖像選擇器中保存圖像,我的圖像選擇器使用defaultRepresentation,還有一個AlAsset在iOS中保存照片的原始文件名

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中,之後(應用程序接近初始狀態),我可以使用它重新加載圖像。

回答

0

Informed u在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]; 
} 
相關問題