drawInRect不读取/保留UIImageView转换

时间:2013-10-31 12:21:14

标签: ios uiimageview cgaffinetransform drawinrect

当我在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;
}

非常感谢任何评论。

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

事实证明,AutoresizingMask改变了矩形

 [slider setAutoresizingMask:UIViewAutoresizingNone];