2013-08-29 5 views
2

나는 약 500 개의 너비와 500 개의 높이를 가진 원이되는 일부 UIButtons의 스크린 샷을 찍고 있습니다. 이것은 완벽하게 작동동그라미의 스크린 샷 찍기

UIGraphicsBeginImageContext(self.Button3.layer.bounds.size); 
[self.Button3.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage* image2 = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

    NSData* data = UIImagePNGRepresentation(image); 
    [data writeToFile:BusinessCardPath atomically:YES]; 

, 그것은 나에게 만 원의 사진없이를 반환 스크린 샷 만 평방 형태로 제조 할 수있다, 나는 이미지 주위에 흰색 모서리를 wan't하지 않았기 때문에이 코드를 사용 흰 모서리. 그러나 픽셀을 잃어 버릴 때 확대 될 수 있으며, UIButton에있는 UILabels와 같은 모든 요소는 촬영에 포함되지 않지만 UIButton의 하위 뷰에는 포함되지 않습니다. 이 코드를 사용하는 경우

는 :

CGSize imageSize = CGSizeMake(310, 310); 

UIGraphicsBeginImageContext(self.Button3.layer.bounds.size); 

CGContextRef context = UIGraphicsGetCurrentContext(); 
/*CGContextTranslateCTM(context, -5, -50);*/ 
// Iterate over every window from back to front 
for (UIWindow *window in [[UIApplication sharedApplication] windows]) 
{ 
    if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) 
    { 
     // -renderInContext: renders in the coordinate space of the layer, 
     // so we must first apply the layer's geometry to the graphics context 
     CGContextSaveGState(context); 
     // Center the context around the window's anchor point 
     CGContextTranslateCTM(context, [window center].x, [window center].y); 
     // Apply the window's transform about the anchor point 
     CGContextConcatCTM(context, [window transform]); 
     // Offset by the portion of the bounds left of and above the anchor point 
     CGContextTranslateCTM(context, 
           -[window bounds].size.width * [[window layer] anchorPoint].x, 
           -[window bounds].size.height * [[window layer] anchorPoint].y); 

     // Render the layer hierarchy to the current context 
     [[window layer] renderInContext:context]; 

     // Restore the context 
     CGContextRestoreGState(context); 
    } 
} 

// Retrieve the screenshot image 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

을 ... 나는 반대의 효과를 얻는다. 위의 요소는 스크린 샷, 고해상도에 포함되어 있지만 이미지 주변의 흰색 모서리가 있습니다.

답변

2

CAShapeLayer를 사용하여 이미지의 일부를 마스크 할 수 있습니다. .path 속성은 렌더링 될 영역을 정의하는 데 사용됩니다. 당신에게 원 이미지를 제공해야

UIImageView *myImageView = [UIImageView alloc] init]; 
myImageView.image = [UIImage imageNamed:@"anImage.gif"]; 
[myImageView setClipsToBounds:YES]; 

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:myImageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(48, 48)]; 
CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
maskLayer.frame = myImageView.bounds; 
maskLayer.path = maskPath.CGPath; 

myImageView.layer.mask = maskLayer; 

. 반경으로 경기해야 할 수도 있습니다.