2011-10-19 2 views
1

이 주제에 대한 정보가 흩어져있는 것 같지만 다소 혼란스러워 보입니다. 그래서 나는 당신이 묻는 것을 꺼리지 않습니다.UIImagePickerController - 이미지 저장 및로드?

UIImagePickerController가 작동하여 사진을 찍거나 라이브러리에서 선택하고 UIImageView에서 촬영 한/선택한 이미지를 표시하도록 선택합니다.

그러나 어떻게 이미지를 먼저 저장 한 다음 응용 프로그램을 다시 열 때 다시로드 할 수 있습니까? 또한 여러 이미지를 저장하여 고유하게 식별해야합니다.

도움을 주시면 대단히 감사하겠습니다.

답변

3

사진 앨범에서 사진을 두 번째로 가져 오는 방법을 모르겠습니다. 다시 검색하는 방법이없는 것처럼 보입니다. 이미지를 내부적으로 저장하는 몇 가지 작업을 수행합니다.

이미지를 NSData로 변환하고 CoreData에 저장하거나 샌드 박스에 저장합니다.

여기에 우리가 사용하는 코드 조각이 있습니다. 이것은 몇 가지 중 하나입니다. ScreenShot에서 이미지를 가져오고 있지만 UIImage를 사용하면 동일합니다.

// 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)]; 

도우미 기능은 왜 다음`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

이상 사용? –

관련 문제