2013-10-31 50 views
1

當我在drawInRect中捏住,平移或旋轉UIImageview時,我遇到了一個問題,該變換未被保留。drawInRect不讀取/保留UIImageView變換

如何保留drawInRect中的變換?

我試過,但沒有去:(

- (UIImage*) combineImage:(UIImageView *)selectedImage withOverlay:(UIImageView *)overlayImage 
{ 
    /* Identify the region that needs to be cropped */ 
    CGRect viewForImgFrame = self.viewForImg.frame; 
    NSLog(@"view %@", NSStringFromCGRect(viewForImgFrame)); 
    NSLog(@"selectedImage Img value %@",selectedImage); 
    NSLog(@"overlayImage Img value %@",overlayImage); 
    CGSize newImageSize =self.viewForImg.frame.size; 
    NSLog(@"CGSize %@",NSStringFromCGSize(newImageSize)); 
    UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0); //retina res 

    //[self.viewForImg.layer renderInContext:UIGraphicsGetCurrentContext()]; 

    [selectedImage.image drawInRect:CGRectMake(0, 0, selectedImage.frame.size.width, selectedImage.frame.size.height)]; 

    CGContextConcatCTM(UIGraphicsGetCurrentContext(), overlayImage.transform); 

    [overlayImage.image drawInRect:CGRectMake(overlayImage.frame.origin.x, overlayImage.frame.origin.y, overlayImage.frame.size.width, overlayImage.frame.size.height)]; 


    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    NSData *imgData = UIImageJPEGRepresentation(image, 0.9); //UIImagePNGRepresentation (image); // get JPEG representation 
    UIImage * imagePNG = [UIImage imageWithData:imgData]; // wrap UIImage around PNG representation 
    UIGraphicsEndImageContext(); 
    return imagePNG; 
} 

任何意見是極大的讚賞。

+0

請顯示整個'drawRect:'方法的實現。 – trojanfoe

+0

嗨trojanfoe,更新謝謝 – Desmond

回答

0

轉過身來out AutoresizingMask改變矩形尺寸

[slider setAutoresizingMask:UIViewAutoresizingNone]; 
1

你需要嘗試

CGRectApplyAffineTransform(<#CGRect rect#>, <#CGAffineTransform t#>) 

你的代碼應該像

CGContextConcatCTM(UIGraphicsGetCurrentContext(), overlayImage.transform); 
CGRect rect = CGRectMake(overlayImage.frame.origin.x, overlayImage.frame.origin.y, overlayImage.frame.size.width, overlayImage.frame.size.height); 
CGRect transformedRect = CGRectApplyAffineTransform(rect, overlayImage.transform); 
[overlayImage.image drawInRect:transformedRect]; 
+0

您好塔爾哈, 感謝您的答覆,不幸的是它的工作。覆蓋層消失了 – Desmond