2012-03-09 87 views
3

我試圖裁剪用CAShapeLayer圖像與此代碼CAShapeLayer croping圖像

CALayer* contentLayer = [CALayer layer]; 
    CAShapeLayer* mask = [CAShapeLayer layer]; 

    CGMutablePathRef path = CGPathCreateMutable(); 

    CGPathMoveToPoint(path, NULL, 10, 10); 
    CGPathAddLineToPoint(path, NULL, 10, 80); 
    CGPathAddLineToPoint(path, NULL, 80, 80); 
    CGPathAddLineToPoint(path, NULL, 80, 10); 
    mask.path = path; 

    [contentLayer setContents:(id)[[UIImage imageNamed:@"image.png"] CGImage]]; 
    [contentLayer setMask:mask];   

    [[self layer]addSublayer:contentLayer]; 

執行這個我能看到的只是空的觀點後,

回答

4

您從未設置您的contentLayerframe,因此它默認爲CGRectZero,這會使其內容不可見。將框架設置爲具有圖像的大小,您應該看到它。