2010-11-16 26 views
12

Tôi chụp ảnh màn hình trong ứng dụng của mình. Tôi có thể chụp ảnh màn hình.UIGraphicsBeginImageContext với tham số

Bây giờ tôi muốn chụp ảnh màn hình bằng cách chỉ định tọa độ x và y. Điều đó có thể không?

UIGraphicsBeginImageContext(self.view.bounds.size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage* aImage = UIGraphicsGetImageFromCurrentImageContext(); 

Trả lời

18
UIGraphicsBeginImageContext(self.view.bounds.size); 
CGContextRef c = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(c, 0, -40); // <-- shift everything up by 40px when drawing. 
[self.view.layer renderInContext:c]; 
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+0

nhờ trả lời ... là có cách nào để chage chiều cao và chiều rộng của nó .. – user198725878

+0

@barbgal: Sử dụng CGContextScaleCTM để thay đổi kích cỡ. Ngoài ra, bạn có thể cần một ngữ cảnh nhỏ hơn (thay đổi đối số thành UIGraphicsBeginImageContext). – kennytm

+2

@Barbgal: Không. 'CGSize size = self.view.bounds.size; UIGraphicsBeginImageContext (CGSizeMake (size.width, size.height-40)); CGContextRef c = UIGraphicsGetCurrentContext(); ' – kennytm

2

Nếu bạn đang sử dụng một thiết bị hiển thị võng mạc mới, mã của bạn nên chú ý tới độ phân giải bằng cách sử dụng UIGraphicsBeginImageContextWithOptions thay vì UIGraphicsBeginImageContext:

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,YES,2.0); 
CGContextRef c = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(c, 0, -40); // <-- shift everything up by 40px when drawing. 
[self.view.layer renderInContext:c]; 
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

Điều này sẽ làm cho bối cảnh hình ảnh hiển thị võng mạc .

0

Chỉ cần làm một cái gì đó như thế này, cách dễ dàng hơn so với tất cả những tính toán phức tạp

+ (UIImage *)imageWithView:(UIView *)view { 
    UIGraphicsBeginImageContextWithOptions([view bounds].size, NO, [[UIScreen mainScreen] scale]); 

    [[view layer] renderInContext:UIGraphicsGetCurrentContext()]; 

    UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return result; 
} 
0

Đây là phiên bản Swift phiên bản

//Capture Screen 
func capture()->UIImage { 

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, UIScreen.mainScreen().scale) 
    self.view.layer.renderInContext(UIGraphicsGetCurrentContext()!) 
    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return image 

} 
0

nhanh chóng

UIGraphicsBeginImageContext(self.view.bounds.size) 
    let image: CGContextRef = UIGraphicsGetCurrentContext()! 
    CGContextTranslateCTM(image, 0, -40) 
    // <-- shift everything up by 40px when drawing. 
    self.view.layer.renderInContext(image) 
    let viewImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil)