2011-10-19 12 views
1

このトピックに関する情報のばらつきがあるようですが、むしろ混乱しているように見えます。UIImagePickerController - イメージを保存して読み込みますか?

UIImagePickerControllerが動作しています。写真を撮るか、ライブラリから選択して、UIImageViewを設定して、撮影/選択した画像を表示します。

しかし、まず画像を保存してから、アプリケーションを開いたときに再び読み込むことはできますか?また、複数の画像を保存して、一意に識別する必要があります。

大変ありがとうございます。ありがとうございます。

答えて

3

写真アルバムから写真を2回目に取得する方法がわかりません。もう一度検索する方法はないようです。イメージを内部的に保存するためにいくつかのことを行います。

イメージをNSDataに変換してCoreDataに保存するか、サンドボックスに保存します。

私たちが使っているコードのスナップショットです。これはいくつかのうちの1つを実行しています。スクリーンショットからイメージを取得していますが、UIImageを持っていれば同じものです。

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

ヘルパー関数なぜあなたは 'UIImageJPEGRepresentation'を使用して、PNGの拡張子で保存されていますか? –

関連する問題