2017-02-06 212 views
0

我使用a tutorial來創建繪圖應用程序的開始。繪圖發生在一個圖像視圖上,並且在筆畫完成後,它將與下面的圖像視圖合併並清除頂部圖像。UIImageView使用不透明繪圖和透明背景繪圖

目前,我的應用程序可以在白色背景上繪製黑色數字。但是,我希望它最終能夠在透明背景上繪製數字,以便在數字背後可以看到主要繪圖視圖背後的任何背景/圖像/ UIView。這可以使用我用過的繪圖方法嗎?

我的.m代碼:

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    // DEFAULT VALUES 
    red = 0.0/255.0; 
    green = 0.0/255.0; 
    blue = 0.0/255.0; 
    brush = 10.0; 
    opacity = 1.0; 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

/////////////// TOUCH RESPONSE METHODS (for drawing) /////////////// 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    mouseSwiped = NO; 
    UITouch *touch = [touches anyObject]; 
    lastPoint = [touch locationInView:self.view]; // first point touched 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    mouseSwiped = YES; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.view]; // second point touched (to be connected to first point) 

    // initialize the tempDrawImage UIImageView that will be drawn on 
    UIGraphicsBeginImageContext(self.view.frame.size); 
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

    // draw a line with CGContextAddLineToPoint from lastPoint to currentPoint 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 

    // set brush size and opacity and brush stroke color 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0); 
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal); 

    // draw the path 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 

    // draw on the tempDrawImage UIImageView 
    self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    [self.tempDrawImage setAlpha:opacity]; 

    UIGraphicsEndImageContext(); 
    lastPoint = currentPoint; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    // if screen was not swiped (i.e. the screen was only tapped), draw a single point 
    if(!mouseSwiped) { 
     UIGraphicsBeginImageContext(self.view.frame.size); 
     [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush); 
     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity); 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextStrokePath(UIGraphicsGetCurrentContext()); 
     CGContextFlush(UIGraphicsGetCurrentContext()); 
     self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 

    ///////BRUSH STROKE IS DONE; BEGIN UIIMAGEVIEW MERGE////// 

    // initialize mainImage (for merge) 
    UIGraphicsBeginImageContext(self.mainImage.frame.size); 

    // merge tempDrawImage with mainImage 
    [self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0]; 
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity]; 
    self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext(); 

    // clear tempDrawImage 
    self.tempDrawImage.image = nil; 

    UIGraphicsEndImageContext(); 
} 

我的.h代碼:

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController { 

    CGPoint lastPoint; // stores the last drawn point on the canvas; used when a continuous brush stroke is being drawn on the canvas. 

    // RGB values 
    CGFloat red; 
    CGFloat green; 
    CGFloat blue; 

    // brush stroke width and opacity 
    CGFloat brush; 
    CGFloat opacity; 

    BOOL mouseSwiped; // identifies if the brush stroke is continuous 
} 

// IMAGE VIEWS 
@property (weak, nonatomic) IBOutlet UIImageView *songSheetBase; 
@property (weak, nonatomic) IBOutlet UIImageView *mainImage; 
@property (weak, nonatomic) IBOutlet UIImageView *tempDrawImage; 

@end 

謝謝!

回答

0

您可以使用正在使用的開始上下文方法的WithOptions變體創建透明圖像上下文(並使其成爲當前上下文),將opaque參數設置爲NO

// change these: 
// UIGraphicsBeginImageContext(self.view.frame.size); 

// to: 
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0); 

[[UIColor clearColor] setFill]; 
CGContextFillRect(context, self.view.bounds); 

請在每次創建爲繪製圖像的背景下,除了地方,我想繪製的合併圖像...

// not here... initialize mainImage (for merge) 
UIGraphicsBeginImageContext(self.mainImage.frame.size); 
+0

謝謝您的答覆。我改變了前兩個UIGraphicsBeginImageContext行,但它似乎沒有固定的東西。我想知道故事板上的UIImageView繪圖設置是否不正確?兩個視圖都不透明,不隱藏,其餘都被檢查。 – Lorraine

+0

嗯。很抱歉聽到。只需編輯添加幾行:setFill然後fillRect。 (我認爲清晰是默認的,但也許你必須在新的上下文中繪製清晰的顏色)。 – danh

+0

這工作!非常感謝你的幫助。 – Lorraine