2014-01-30 21 views
2

在我的應用程序中,我有一個名爲allView的添加子視圖的滾動視圖。 在滾動視圖的委託方法我申請滾動視圖的子視圖的當前轉換的價值,另一種觀點叫做漆視圖如何將UIScrollView的轉換應用於UIView?

paintView.transform = allView.transform 

,並將其保存到磁盤。

在該過程的結果中創建的圖像看起來不同於屏幕上的圖像。爲什麼?我該如何解決它?

視圖控制器

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;            { 
    self.paintView.transform =self.allView.transform; 
    [self.paintView setNeedsDisplay]; 
} 
    - (void)scrollViewDidZoom:(UIScrollView *)scrollView{ 
     self.backgroundView.transform = self.allView.transform; 
     [self.paintView setNeedsDisplay]; 
    } 

油漆查看

裏面的PaintView的抽籤矩形我試圖從滾動視圖應用轉型

- (void)drawRect:(CGRect)rect 
{ 

// Drawing code 
// Draw on the screen 
    CGContextRef ctx1 =UIGraphicsGetCurrentContext(); 
    CGContextConcatCTM(ctx1, self.transform); 

    CGColorRef wh = [[UIColor redColor]CGColor]; 
    CGContextSetStrokeColorWithColor(ctx1, wh); 
    CGContextMoveToPoint(ctx1, 0, 0); 
    CGContextAddLineToPoint(ctx1, 200, 200); 
    CGContextStrokePath(ctx1); 

    // Apply scroll view's transformation 
    CGRect r = CGRectApplyAffineTransform(rect,self.transform); 

    //that gives a resized image 
    UIGraphicsBeginImageContextWithOptions(r.size, NO, 0.0); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextConcatCTM(ctx, self.transform); 
    // stroke and so on   
    CGContextSetStrokeColorWithColor(ctx, wh); 
    CGContextMoveToPoint(ctx, 0, 0); 
    CGContextAddLineToPoint(ctx, 200, 200); 
    CGContextStrokePath(ctx); 

    Getting image with entire content. 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    //Clipping the image 
    CGImageRef cgImg = CGImageCreateWithImageInRect(image.CGImage, rect); 
    UIImage *img = [UIImage imageWithCGImage:cgImg]; 
    NSData * d = UIImageJPEGRepresentation(img, 0.8); 
    //saving the image (for debugging) 
    [self save:d]; 
    UIGraphicsEndImageContext();  
} 

iPhone模擬器 Desired Effect

圖像保存到磁盤 Result

+0

這是否只發生在視網膜屏幕上? – andreamazz

回答

1

它可能是一個有點難以這裏診斷問題,而不能有正在運行的代碼。不過,我認爲問題可能出在這些線路上你在哪裏裁剪圖像:

// Clipping the image 
CGImageRef cgImg = CGImageCreateWithImageInRect(image.CGImage, rect); 
UIImage *img = [UIImage imageWithCGImage:cgImg]; 

我覺得你其實只是想獲得滾動視圖的可見矩形,這將是scrollView.frame,而不是填充子視圖滾動視圖的整個contentSize。因此,使用某種方式(如paintViewvisibleFrame屬性),我將修訂線是這樣的:

// Clipping the image 
CGImageRef cgImg = CGImageCreateWithImageInRect(image.CGImage, self.visibleFrame); 
UIImage *img = [UIImage imageWithCGImage:cgImg]; 

希望這有助於你獲得通過這個問題!

相關問題