2015-10-23 53 views
0

我正在使用相機選取器控制器拍攝圖片,並將其顯示在我的應用程序中。但是我沒有使用UIImageView來顯示圖片。我在UIView上繪製了UIImage,我得到的結果是顛倒的圖像。下面是截圖:UIImage在UIView上繪製時反轉

enter image description here

後,我點擊使用照片結果:

enter image description here

這裏是我的代碼:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; 

    dView=[[drawView alloc]initWithFrame:imageUIView.frame]; 
    [dView drawImage:chosenImage]; 

    [imageUIView addSubview:dView]; 

    [picker dismissViewControllerAnimated:YES completion:NULL]; 

} 

和DVIEW:

-(void)drawImage:(UIImage *) imageToDraw{ 
    self.pic=imageToDraw; 
    [imageToDraw drawInRect:self.frame]; 
    [self setNeedsDisplay]; 

} 

- (void)drawRect:(CGRect)rect { 

CGContextRef context = UIGraphicsGetCurrentContext(); 

CGRect targetBounds = self.layer.bounds; 
    // fit the image, preserving its aspect ratio, into our target bounds 
CGRect imageRect = AVMakeRectWithAspectRatioInsideRect(self.pic.size, 
                  targetBounds); 
    // draw the image 
CGContextDrawImage(context, imageRect, self.pic.CGImage); 

// Save the screen to restore next time around 
imageRef = CGBitmapContextCreateImage(context); 

} 

我試過CGAffineTransformMakeRotationCGContextRotateCTM以正確顯示圖像,但它們旋轉圖像,而不是按正確的順序顯示內容。正如你所看到的,圖像中的內容完全顛倒了。

如何繪製uiimage而不作任何更改?

+0

@picciano爲什麼?這沒有關係。 – rmaddy

回答

1

您的drawView類沒有正確地繪製圖像。試試這個:

- (void)drawImage:(UIImage *)imageToDraw { 
    self.pic = imageToDraw; 
    [self setNeedsDisplay]; 
} 

- (void)drawRect:(CGRect)rect { 
    CGRect targetBounds = self.layer.bounds; 
     // fit the image, preserving its aspect ratio, into our target bounds 
    CGRect imageRect = AVMakeRectWithAspectRatioInsideRect(self.pic.size, 
                   targetBounds); 
     // draw the image 
    [self.pic drawInRect:imageRect]; 
} 
  1. 這可能是更好的一個UIImageView添加爲drawView的子視圖。讓圖像視圖完成所有需要的工作以正確地呈現圖像。
  2. 命名約定。類名稱應以大寫字母開頭。變量和方法名稱以小寫字母開頭。
+0

我可以使用imageview,但是,我還有其他與通過觸摸清除圖層相關的內容。我無法在imageview上做到這一點,因此即時通訊使用uiview。 ANYWAY我會試試這個! –