2009年5月26日火曜日

Good morning everyone !

I fixed and did analysis of the my native application "YubiFude".

I fixed problem about an algorithm (order of drawing to specified layer).

I completed it. CGLayer objects are good working now.



I did analysis of code of the "YubiFude" application about save and import a image.

It's complication for me.

I don't understand some of this. therefore I'll study it.

But becouse of I could understand some of this, I post the code with comment.

*******************************************************************************

//**********************************************************************
//*******************↓↓↓Save Image to library↓↓↓************************
//**********************************************************************

- (void)saveViewToPhotoLibrary:(id)sender {
    
  CGRect screenRect = [[UIScreen mainScreen] bounds];
    //↑↑↑get the size of window(w:320 h:480)
  UIGraphicsBeginImageContext(screenRect.size);
    //↑↑↑Creates a bitmap-based graphics context and makes it the current context.
    // The drawing environment is pushed onto the graphics context stack immediately.
  CGContextRef ctx = UIGraphicsGetCurrentContext();
    //Returns the current graphics context.ビットマップコンテキストの収集
    //[[UIColor blackColor] set];
  CGContextFillRect(ctx, screenRect);
    //↑↑↑Paints the area contained within the provided rectangle, using the fill color in the current graphics state.
    
  [self.paintingViewController.view.layer renderInContext:ctx];
    //↑↑↑Renders the receiver and its sublayers into the specified context.
    //UIView class has a layer property.It is a CALayer class object 
   
  UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
    //↑↑↑Returns an image based on the contents of the current bitmap-based graphics context.
    
  UIImageWriteToSavedPhotosAlbum(screenImage, 
                                 nil//The object whose selector should be called after the image has been written to the user’s device.
                                 , nil//The selector of the target object to call. This method should be of the form:                        
                                 , nil//An optional pointer to any context-specific data that you want passed to the completion selector.
    );
    //↑↑↑Adds the specified image to the user’s Saved Photos album.
    
  UIGraphicsEndImageContext();    
    //↑↑↑Removes the current bitmap-based graphics context from the top of the stack.
}
//**********************************************************************
//*******************↑↑↑Save Image to library↑↑↑************************
//**********************************************************************


*******************************************************************************
//↓↓↓pushed a loadButton
- (void)showCameraSheet:(id)sender
{
    statusButton.hidden = YES;
    colorButton.hidden = YES;
    fillColorButton.hidden = YES;
    doneButton.hidden = YES;
    allClearButton.hidden = YES;
    layer1ClearButton.hidden = YES;
    layer2ClearButton.hidden = YES;
    layer3ClearButton.hidden = YES;
    saveButton.hidden = YES;
    loadButton.hidden = YES;
    
  // アクションシートを作る:下から出てくるアラートみたいなやつ
  UIActionSheet* sheet;
  sheet = [[UIActionSheet alloc] 
             initWithTitle:@"Add image to current layer" 
             delegate:self 
             cancelButtonTitle:nil 
             destructiveButtonTitle:nil 
             otherButtonTitles:@"Photo Library", @"Saved Photos",@"Cancel" ,nil];
  [sheet autorelease];
    [sheet showInView:self.view];
    
}

- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
  // ボタンインデックスをチェックする、どのボタン押された?
  if (buttonIndex >= 3) {
  return;
  }
    UIImagePickerControllerSourceType sourceType = 0;
  switch (buttonIndex) {
        case 0: {
            sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            // イメージピッカーを作る
            UIImagePickerController* imagePicker;
            imagePicker = [[UIImagePickerController alloc] init];
            [imagePicker autorelease];
            imagePicker.sourceType = sourceType;
            imagePicker.allowsImageEditing = YES;
            imagePicker.delegate = self;
            
            // イメージピッカーを表示する、写真選ぶ画面
            //モーダルな◆ユーザが応答しない限り、そのプログラムの他のコントロールは入力を受け付けない
            [self presentModalViewController:imagePicker animated:YES];
            
            
            break;
        }
        case 1: {
            sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            // イメージピッカーを作る
            UIImagePickerController* imagePicker;
            imagePicker = [[UIImagePickerController alloc] init];
            [imagePicker autorelease];
            imagePicker.sourceType = sourceType;
            imagePicker.allowsImageEditing = YES;
            imagePicker.delegate = self;
            
            // イメージピッカーを表示する
            [self presentModalViewController:imagePicker animated:YES];
            
            break;
        }
        case 2: {//自作キャンセルボタン、デフォルトだとメソッド追加命令不可能
            statusButton.hidden = NO;
            colorButton.hidden = NO;
            fillColorButton.hidden = NO;
            doneButton.hidden = NO;
            allClearButton.hidden = NO;
            layer1ClearButton.hidden = NO;
            layer2ClearButton.hidden = NO;
            layer3ClearButton.hidden = NO;
            saveButton.hidden = NO;
            loadButton.hidden = NO;
            
            [self dismissModalViewControllerAnimated:YES];
            //ModalViewControllerの除去
            break;
        }
            
  }
   
  // 使用可能かどうかチェックする(カメラはipodtouchにはないので)
  if (![UIImagePickerController isSourceTypeAvailable:sourceType]) {
        //Returns a Boolean value indicating whether the device supports picking images using the specified source.
  return;
  }
}

- (void)imagePickerController:(UIImagePickerController*)picker 
  didFinishPickingImage:(UIImage*)image 
                 editingInfo:(NSDictionary*)editingInfo
{
    NSLog(@"editingInfo:%@",editingInfo);
    // UIImagePickerControllerCropRect = NSRect: {{0, 0}, {640, 425}};
  // UIImagePickerControllerOriginalImage = ;

    statusButton.hidden = NO;
    colorButton.hidden = NO;
    fillColorButton.hidden = NO;
    doneButton.hidden = NO;
    allClearButton.hidden = NO;
    layer1ClearButton.hidden = NO;
    layer2ClearButton.hidden = NO;
    layer3ClearButton.hidden = NO;
    saveButton.hidden = NO;
    loadButton.hidden = NO;
    
  // イメージピッカーを隠す
  [self dismissModalViewControllerAnimated:YES];
   
  // オリジナル画像を取得する
  UIImage* originalImage;
  originalImage = [editingInfo objectForKey:UIImagePickerControllerOriginalImage];
   
  // グラフィックスコンテキストを作る、小さくすると無理に引き延ばすのでモザイクがかかる
  CGSize size = { 320, 480 };
  UIGraphicsBeginImageContext(size);
    //Creates a bitmap-based graphics context and makes it the current context.
    

   
  // 画像を縮小して描画する
  CGRect rect;
  rect.origin = CGPointZero;
  rect.size = size;
  [originalImage drawInRect:rect];
  // 描画した画像を取得する
  shrinkedImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
   
  // CGImageを取得する
  CGImageRef cgImage;
  cgImage = shrinkedImage.CGImage;
   
  // 画像情報を取得する
  size_t width;
  size_t height;
  size_t bitsPerComponent;
  size_t bitsPerPixel;
  size_t bytesPerRow;
  CGColorSpaceRef colorSpace;
  CGBitmapInfo bitmapInfo;
  bool shouldInterpolate;
  CGColorRenderingIntent intent;
  width = CGImageGetWidth(cgImage);
  height = CGImageGetHeight(cgImage);
  bitsPerComponent = CGImageGetBitsPerComponent(cgImage);
  bitsPerPixel = CGImageGetBitsPerPixel(cgImage);
  bytesPerRow = CGImageGetBytesPerRow(cgImage);
  colorSpace = CGImageGetColorSpace(cgImage);
  bitmapInfo = CGImageGetBitmapInfo(cgImage);
  shouldInterpolate = CGImageGetShouldInterpolate(cgImage);//Returns the interpolation setting for a bitmap image.
  intent = CGImageGetRenderingIntent(cgImage);//Returns the rendering intent setting for a bitmap image.
   
  // データプロバイダを取得する
  CGDataProviderRef dataProvider;
  dataProvider = CGImageGetDataProvider(cgImage);//Returns the data provider for a bitmap image.
    

   
  // ビットマップデータを取得する
  CFDataRef data;
  UInt8* buffer;
  data = CGDataProviderCopyData(dataProvider);
  buffer = (UInt8*)CFDataGetBytePtr(data);
  // ビットマップに効果を与える
    /* NSUInteger i, j;
     for (j = 0; j < height; j++) {
     for (i = 0; i < width; i++) {
     // ピクセルのポインタを取得する
     UInt8* tmp;
     tmp = buffer + j * bytesPerRow + i * 4;
     
     // RGBの値を取得する
     UInt8 r, g, b;
     r = *(tmp + 3);
     g = *(tmp + 2);
     b = *(tmp + 1);
     
     // 輝度値を計算する
     UInt8 y;
     y = (77 * r + 28 * g + 151 * b) / 256;
     
     // 輝度の値をRGB値として設定する
     *(tmp + 1) = y;
     *(tmp + 2) = y;
     *(tmp + 3) = y;
     }
     }
     */
  // 効果を与えたデータを作成する
  CFDataRef effectedData;
  effectedData = CFDataCreate(NULL, buffer, CFDataGetLength(data));
   
  // 効果を与えたデータプロバイダを作成する
  CGDataProviderRef effectedDataProvider;
  effectedDataProvider = CGDataProviderCreateWithCFData(effectedData);
   
  // 画像を作成する
    // UIImage* effectedImage;
  effectedCgImage = CGImageCreate(
                                    width, height, 
                                    bitsPerComponent, bitsPerPixel, bytesPerRow, 
                                    colorSpace, bitmapInfo, effectedDataProvider, 
                                    NULL, shouldInterpolate, intent);
    // effectedImage = [[UIImage alloc] initWithCGImage:effectedCgImage];
    // [effectedImage autorelease];
   
  // 画像を表示する
    // _imageView.image = effectedImage;

  [PaintingView loadImage];
  // 作成したデータを解放する
  CGImageRelease(effectedCgImage);
  CFRelease(effectedDataProvider);
  CFRelease(effectedData);
  CFRelease(data);
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
  // イメージピッカーを隠す
  [self dismissModalViewControllerAnimated:YES];
}

*****************************************************************************


0 件のコメント:

コメントを投稿