2012-10-02 46 views
12

Tôi có một ứng dụng mà phải mất một ảnh chụp màn hình của một UIImageView với đoạn mã sau:iOS Ảnh chụp màn hình một phần của màn hình

-(IBAction) screenShot: (id) sender{ 

UIGraphicsBeginImageContext(sshot.frame.size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(viewImage,nil, nil, nil); 


} 

này hoạt động tốt nhưng tôi cần để có thể xác định vị trí nơi tôi lấy ảnh chụp màn hình về cơ bản tôi chỉ cần grad một phần ba màn hình (phần giữa). Tôi đã thử sử dụng

UIGraphicsBeginImageContext(CGSize 150,150); 

Nhưng đã nhận thấy rằng mọi thứ được lấy từ 0,0 tọa độ, có bất kỳ ai biết cách đặt vị trí này chính xác.

+0

Vấn đề tương tự cũng xảy ra đối với tôi, tôi không thể tìm thấy giải pháp nào cho nó. –

Trả lời

29

Ảnh chụp màn hình được lấy từ canvas bạn vẽ. Vì vậy, thay vì vẽ lớp của bạn trong toàn bộ bối cảnh, với một tham chiếu đến đầu góc trái, bạn sẽ vẽ nó nơi bạn muốn lấy ảnh chụp màn hình ....

//first we will make an UIImage from your view 
UIGraphicsBeginImageContext(self.view.bounds.size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

//now we will position the image, X/Y away from top left corner to get the portion we want 
UIGraphicsBeginImageContext(sshot.frame.size); 
[sourceImage drawAtPoint:CGPointMake(-50, -100)]; 
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(croppedImage,nil, nil, nil); 
+0

Cảm ơn sự giúp đỡ của bạn. – yeha

+2

'sshot.frame.size' là gì? Ý bạn là 'self.view.frame.size'? – progrmr

+0

Đó là kích thước của ảnh chụp màn hình. Bạn có thể thay đổi dòng đó thành 'UIGraphicsBeginImageContext (CGSizeMake (200.200)); ' để tạo ra hình ảnh chụp màn hình 200X200 – Lefteris

14

Từ this

UIGraphicsBeginImageContext(sshot.frame.size); 
CGContextRef c = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(c, 150, 150); // <-- shift everything up to required position when drawing. 
[self.view.layer renderInContext:c]; 
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 
+0

Có, đây thực sự là giải pháp tốt hơn – Lefteris

+0

có vẻ như là một giải pháp tốt. –

7

Sử dụng phương pháp này để cắt nếu u có hình ảnh với rect specfic để cắt:

-(UIImage *)cropImage:(UIImage *)image rect:(CGRect)cropRect 
{ 
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect); 
    UIImage *img = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
    return img; 
} 

Sử dụng như thế này:

UIImage *img = [self cropImage:viewImage rect:CGRectMake(150,150,100,100)]; //example 
+0

Cảm ơn sự giúp đỡ của bạn. – yeha

0

Nếu bạn thích, bạn có thể tham khảo this mã.

Trong ví dụ này, bạn có thể lấy hình ảnh được bao phủ bởi trực tràng từ bất kỳ vị trí nào và bất kỳ tỷ lệ thu phóng nào.

Chúc mừng Mã hóa :)

Một số mã được trích xuất để tham khảo như sau

chức năng chính hoặc mã được sử dụng để cắt ảnh

- (UIImage *) croppedPhoto 
{ 
    CGFloat ox = self.scrollView.contentOffset.x; 
    CGFloat oy = self.scrollView.contentOffset.y; 
    CGFloat zoomScale = self.scrollView.zoomScale; 
    CGFloat cx = (ox + self.cropRectangleButton.frame.origin.x + 15.0f) * 2.0f/zoomScale; 
    CGFloat cy = (oy + self.cropRectangleButton.frame.origin.y + 15.0f) * 2.0f/zoomScale; 
    CGFloat cw = 300.0f/zoomScale; 
    CGFloat ch = 300.0f/zoomScale; 
    CGRect cropRect = CGRectMake(cx, cy, cw, ch); 

    NSLog(@"---------- cropRect: %@", NSStringFromCGRect(cropRect)); 
    NSLog(@"--- self.photo.size: %@", NSStringFromCGSize(self.photo.size)); 

    CGImageRef imageRef = CGImageCreateWithImageInRect([self.photo CGImage], cropRect); 
    UIImage *result = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 

    NSLog(@"------- result.size: %@", NSStringFromCGSize(result.size)); 

    return result; 
} 

Các chi tiết làm thế nào để sử dụng ví dụ được đưa ra here .

Tận hưởng mã hóa :)

Các vấn đề liên quan