2012-01-02 33 views
16

的截圖一樣,你可以在我的代碼看,我採取截圖並保存到相冊。編程採取特定區域

//for retina displays 
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale); 
} else { 
    UIGraphicsBeginImageContext(self.view.bounds.size); 
} 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 

起初我用webview.size代替self.view.bounds.size和它正常工作,因爲有觀點位於0/0。但是現在我集中了WebView,但對於給定尺寸,圖片從0/0開始。

如何配置屏幕截圖在給定大小的另一個location(例如300/150)處開始?

或者有另一種方式來採取UIWebView的照片嗎?

+0

這是否在桌面OSX或只是iphone的工作? – Noitidart 2015-05-07 19:53:32

回答

2

我解決我的問題,但沒有回答這個問題:

我創建的UIView子類,並動了我的UIWebView在那裏,所以它相對於子類位於0/0。然後使用WebView的大小:

UIGraphicsBeginImageContext(webview.frame.size); 

和上面的其他代碼。現在

您可以在子類移動到你希望每一個位置。

備註:如果您有類似日期/時間標籤的標籤,您也必須將其移動,否則您將無法在圖片上看到它們。

因此,創建的UIView一個子類,每移動IBOutlet你想成爲 看到在那裏的圖片。

問題仍然存在: 那麼有可能拍攝一個特定的屏幕區域?

親切的問候。 $ H @ RKY

0
- (void)drawRect:(CGRect)rect { 
    UIGraphicsBeginImageContext(self.bounds.size);  
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 
} 

你可以打電話給你的觀點:]

+0

創建Web視圖時會調用drawRect方法 - 所以在開始時我有一張灰色的圖片。 我從這得到的想法是拖動代碼並將其放置在我的Web視圖位於0/0的自定義UIView類中。這工作,但我沒有看到標籤了,因爲它是「根視圖」的一部分。 – Sharky 2012-01-03 07:28:56

23

你需要做兩個變化,以只抓取屏幕的一部分:

  1. 創建一個更小的圖像 - 使用你想拍攝的矩形的大小。
  2. 移動,你渲染成是你想拍攝的矩形負的x/y的上下文的原點。

之後,您只需將圖層渲染到上下文中,然後就可以獲得所需的內容。像這樣的事情應該這樣做:

CGRect grabRect = CGRectMake(40,40,300,200); 

//for retina displays 
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
    UIGraphicsBeginImageContextWithOptions(grabRect.size, NO, [UIScreen mainScreen].scale); 
} else { 
    UIGraphicsBeginImageContext(grabRect.size); 
} 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(ctx, -grabRect.origin.x, -grabRect.origin.y); 
[self.view.layer renderInContext:ctx]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 
+0

這使我的應用崩潰。任何想法爲什麼?我已經添加了QuartzCore框架,並將這些代碼放在一個函數中,但仍然沒有結束。 – 2013-06-05 13:59:15

+0

@rebellion - 我已經得到這個工作。儘管不在相冊中。看到我的改編如下UIView類別的方法。 – idStar 2013-06-20 22:02:20

+0

謝謝,這對我很好。只是一個小問題的圖像背景顏色是白色的,我希望它透明。我怎樣才能做到這一點。請幫忙 – iMemon 2013-11-06 14:22:01

10

我已經採取和測試@ escrafford的答案上面,並得到它的工作就好了。我在UIView中將它作爲一個類別方法的上下文中進行了測試,以便'grabRect'可以被參數化。下面是代碼,作爲UIView類別,返回給你一個UIImageView:

/** 
Takes a screenshot of a UIView at a specific point and size, as denoted by 
the provided croppingRect parameter. Returns a UIImageView of this cropped 
region. 

CREDIT: This is based on @escrafford's answer at http://stackoverflow.com/a/15304222/535054 
*/ 
- (UIImageView *)rp_screenshotImageViewWithCroppingRect:(CGRect)croppingRect { 
    // For dealing with Retina displays as well as non-Retina, we need to check 
    // the scale factor, if it is available. Note that we use the size of teh cropping Rect 
    // passed in, and not the size of the view we are taking a screenshot of. 
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
     UIGraphicsBeginImageContextWithOptions(croppingRect.size, YES, [UIScreen mainScreen].scale); 
    } else { 
     UIGraphicsBeginImageContext(croppingRect.size); 
    } 

    // Create a graphics context and translate it the view we want to crop so 
    // that even in grabbing (0,0), that origin point now represents the actual 
    // cropping origin desired: 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextTranslateCTM(ctx, -croppingRect.origin.x, -croppingRect.origin.y); 
    [self.layer renderInContext:ctx]; 

    // Retrieve a UIImage from the current image context: 
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    // Return the image in a UIImageView: 
    return [[UIImageView alloc] initWithImage:snapshotImage]; 
} 
+0

這個代碼當然是假設ARC,並且是iOS6.1中的當前工作解決方案。 – idStar 2013-06-20 22:04:08

+1

偉大的解決方案!謝謝! – MaKo 2013-11-05 01:02:47