2017-09-06 108 views
1

enter image description hereios如何刪除黑色邊框,當我採取截圖(目標c)?

enter image description here

我不想黑色邊框當我採取的截圖,這是我作爲圖像輸出。我用這個代碼輸出圖像(的XCode 6.2客觀C) 代碼:

UIGraphicsBeginImageContext(self.outputView.bounds.size); 
    [self.outputView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    UIGraphicsBeginImageContextWithOptions(self.outputView.bounds.size, self.outputView.opaque,0.0); 
    [sourceImage drawAtPoint:CGPointMake(0,0)]; 
    UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    UIImageWriteToSavedPhotosAlbum(croppedImage,nil, nil, nil); 
    NSData *pngData2 = UIImagePNGRepresentation(croppedImage); 
    NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsPath2 = [paths2 objectAtIndex:0]; 
    NSString *filePath2 = [documentsPath2 stringByAppendingPathComponent:@"screen.png"]; 
    [pngData2 writeToFile:filePath2 atomically:YES]; 

回答

0

請嘗試使用此代碼。

對於斯威夫特

func takeScreenShot() -> UIImage 
{ 
     UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0); 
     self.view.drawHierarchy(in: view.bounds, afterScreenUpdates: true) 
     let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
     UIGraphicsEndImageContext() 
     return image 
} 

對於目標C

- (UIImage *)takeScreenShot 
{ 
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 2.0f); 
    [self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return image; 
} 

它爲我工作。

1

你可以做的是創建單獨或者與實際尺寸複製的UIView和減雙邊框寬度的像素和複製查看進程和採取重複視圖的屏幕截圖。

使用以下代碼進行截圖。

- (UIImage *)takeSnapshotOfHighlightingView:(UIView *)originalView { 
    UIGraphicsBeginImageContextWithOptions(originalView.bounds.size, NO, 0); 
    [originalView drawViewHierarchyInRect:originalView.bounds afterScreenUpdates:YES]; 
    UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return screenShot; 
}