2017-09-06 111 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; 
}