2011-10-19 56 views
1

似乎有關於這個主題的信息分散,但它似乎相當混亂,所以我鋤頭你不介意我問。UIImagePickerController - 保存並加載圖像?

我有一個UIImagePickerController工作,讓我拍照或從庫中選擇,然後設置我選擇的UIImageView來顯示拍攝/選擇的圖像。

但是,如何首先保存圖像,然後在應用程序重新打開時重新載入圖像?另外,我將保存多個圖像,以便它們需要唯一標識。

任何幫助將大規模讚賞,謝謝。

回答

3

我不知道如何從照片相冊第二次獲取照片。似乎沒有辦法再次檢索它。我們會在內部執行一些操作來保存圖像。

我們將圖像轉換爲NSData並將其保存在CoreData中,或者將其保存到我們的沙箱中。

這裏是我們使用的代碼片段,這是做了幾件事情之一,我從ScreenShot獲取圖像,儘管它有相同的一旦你有UIImage。上面爲什麼你使用`UIImageJPEGRepresentation`,然後用一個PNG擴展節省

//-------------------------------------------------------------------------------------------------------- 
    // saveImage:withName: 
    // Description: Save the Image with the name to the Documents folder 
    // 
    //-------------------------------------------------------------------------------------------------------- 

- (void)saveImage:(UIImage *)image withName:(NSString *)name { 

     //grab the data from our image 
    NSData *data = UIImageJPEGRepresentation(image, 1.0); 

     //get a path to the documents Directory 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

     // Add out name to the end of the path with .PNG 
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", name]]; 

     //Save the file, over write existing if exists. 
    [fileManager createFileAtPath:fullPath contents:data attributes:nil]; 

} 

    //-------------------------------------------------------------------------------------------------------- 
    // createScreenShotThumbnailWithWidth 
    // Description: Grab a screen shot and then scale it to the width supplied 
    // 
    //-------------------------------------------------------------------------------------------------------- 

-(UIImage *) createScreenShotThumbnailWithWidth:(CGFloat)width{ 
     // Size of our View 
    CGSize size = self.view.bounds.size; 

     //First Grab our Screen Shot at Full Resolution 
    UIGraphicsBeginImageContext(size); 
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 


     //Calculate the scal ratio of the image with the width supplied. 
    CGFloat ratio = 0; 
    if (size.width > size.height) { 
     ratio = width/size.width; 
    } else { 
     ratio = width/size.height; 
    } 

     //Setup our rect to draw the Screen shot into 
    CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height); 

     //Scale the screenshot and save it back into itself 
    UIGraphicsBeginImageContext(rect.size); 
    [screenShot drawInRect:rect]; 
    screenShot = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

     //Send back our screen shot 
    return screenShot; 

} 
+0

使用

// go get our screenshot UIImage* screenShot = [self createScreenShotThumbnailWithWidth:200]; // Save screen shot as a png in the documents directory with the UUID of the IoCDScreen // Saving to Sandbox [self saveImage:screenShot withName:currentScreen.itemUUID]; // save image to Photo Album - though you can't get a ref back to it UIImageWriteToSavedPhotosAlbum(screenShot, self, nil, nil); //convert our screen shot PNG to NSData and store it in a CoreData Managed Object currentScreen.screenImageDevelopment = [NSData dataWithData: UIImagePNGRepresentation(screenShot)]; 

輔助函數? –