我试图AVPlayer在一个更大的视图内截取屏幕截图.我想只构建一个测试框架,所以私有APIs或任何方法都是好的,因为在发布到AppStore时不会包含框架.
我试过用
UIGetScreenImage() :在模拟器上运行良好但在设备上运行不正常 snapshotviewafterscreenupdates:它显示视图,但我不能创建一个UIImage.drawViewInHierarchy并且renderInContext不会合作AVPlayer AVAssetGenerator用于从视频中获取图像,很难获得良好的坐标或视频播放器作为其他视图的子视图我有一个名为overView的UIView:
overView.frame = CGRectMake(self.view.frame.width/25, self.view.frame.height/25, self.view.frame.width/1.3, self.view.frame.height/1.2)
Run Code Online (Sandbox Code Playgroud)
我想仅截取此视图的屏幕截图,而不是整个屏幕.并制作尺寸截图:
(CGSizeMake(2480,3508 )
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(2480,3508 ), false, 0);
self.view.drawViewHierarchyInRect(CGRectMake(-self.view.frame.width/25, -self.view.frame.height/25,2480,3508), afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext()
Run Code Online (Sandbox Code Playgroud)
屏幕截图具有所需的大小,但它需要整个视图的屏幕截图,而不仅仅是"overView".
在目标C中我们是否可以拍摄屏幕截图并将此图像存储在UIImage中.
问题
我正在构建一系列照片拼贴画,这些照片是我放在桌面视图上的.当图像数量达到tableview单元格宽度的边界时,我想让图像换行(这样我就可以在拼贴中显示图像行).目前我得到一排.如果需要其他信息,请随时告知.我很可能不会以最有效的方式接近这一点,因为随着阵列中使用的图像数量开始增加而存在延迟.(对此的任何反馈将非常感激).
Nota Bene
我正在创建拼贴图像.它实际上是一个图像.我想通过在内存中创建一个有效的列和行矩阵来安排拼贴 .然后我用图像填充这些图像.最后,我对生成的图像进行快照,并在需要时使用它.算法写入效率不高,只产生一行图像.我需要一个轻量级替代下面使用的算法.在这种情况下,我不相信UICollectionView会是一个有用的替代品.
伪代码
使用:Swift 2.0
class func collageImage (rect:CGRect, images:[UIImage]) -> UIImage {
let maxSide = max(rect.width / CGFloat(images.count), rect.height / CGFloat(images.count))
UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.mainScreen().scale)
var xtransform:CGFloat = 0.0
for img in images {
let smallRect:CGRect = CGRectMake(xtransform, 0.0,maxSide, maxSide)
let rnd = arc4random_uniform(270) + 15
//draw in rect
img.drawInRect(smallRect)
//rotate img using random angle.
UIImage.rotateImage(img, radian: CGFloat(rnd))
xtransform += CGFloat(maxSide * 0.8)
}
let …Run Code Online (Sandbox Code Playgroud) 我尝试了所建议的但是输出是一个白色的空白截图.这让我想到我没有在视图中添加任何内容.这是我在视图中添加图形的方法.addChild方法附带SpriteKit,它接收SKSpriteNodes:
addChild(background)
addChild(rate)
addChild(scoreLabel)
addChild(share)
addChild(playAgain)
addChild(highScoreLabel)
addChild(scoreBackground)
addChild(highScoreBackground)
Run Code Online (Sandbox Code Playgroud)
以下是截取屏幕截图的方法:
UIGraphicsBeginImageContext(self.view!.bounds.size)
self.view!.layer.renderInContext(UIGraphicsGetCurrentContext())
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
Run Code Online (Sandbox Code Playgroud)
任何的意见都将会有帮助
你知道如何在UfmageView中保存pdf文件的第一页吗?我必须为pdf创建预览.你有什么主意吗?
感谢Nicco的帮助
我正在尝试实现Range Slider,我使用名为NMRangeSlider的自定义控件.
但是当我使用它时,滑块根本不会出现.难道也是因为它全部是用Objective-C编写的吗?
这就是我目前的实现方式:
var rangeSlider = NMRangeSlider(frame: CGRectMake(16, 6, 275, 34))
rangeSlider.lowerValue = 0.54
rangeSlider.upperValue = 0.94
self.view.addSubview(rangeSlider)
Run Code Online (Sandbox Code Playgroud) 我有一个使用UITableView布局的应用程序.界面占用的不仅仅是屏幕,因此可以滚动到某些区域.我想要一些用于文档和征求客户反馈的整个视图(包括不可见区域)的截图.
是否有编程方式来获取整个视图的图像?我不认为设备上会有这样的方法,但也许有.
如何截取 AVplayerLayer 的截图。我尝试使用以下代码效果很好,它可以按原样捕获整个视图
func screenShotMethod() {
let window = UIApplication.shared.delegate!.window!!
//capture the entire window into an image
UIGraphicsBeginImageContextWithOptions(window.bounds.size, false, UIScreen.main.scale)
window.drawHierarchy(in: window.bounds, afterScreenUpdates: false)
let windowImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//now position the image x/y away from the top-left corner to get the portion we want
UIGraphicsBeginImageContext(view.frame.size)
windowImage?.draw(at: CGPoint(x: -view.frame.origin.x, y: -view.frame.origin.y))
let croppedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext();
//embed image in an imageView, supports transforms.
let resultImageView = UIImageView(image: croppedImage)
UIImageWriteToSavedPhotosAlbum(croppedImage, nil, nil, nil)
}
Run Code Online (Sandbox Code Playgroud)
但问题是当我尝试在 iPhone(设备)上运行相同的代码时,它返回黑色图像。我不知道出了什么问题
任何建议都会非常有帮助!
我想在 UIImage 的右上角或右下角绘制一个文本。我有一个扩展程序可以很好地绘制文本,但问题是将文本定位在屏幕右侧(因为文本被剪切)。
这是扩展名:
extension UIImage {
func addText(drawText: NSString, atPoint: CGPoint, textColor: UIColor?, textFont: UIFont?) -> UIImage{
// Setup the font specific variables
var _textColor: UIColor
if textColor == nil {
_textColor = UIColor.white
} else {
_textColor = textColor!
}
var _textFont: UIFont
if textFont == nil {
_textFont = UIFont.systemFont(ofSize: 50)
} else {
_textFont = textFont!
}
// Setup the image context using the passed image
UIGraphicsBeginImageContext(size)
// Setup the font attributes that will be later …Run Code Online (Sandbox Code Playgroud) 我想将iPhone上的本地html文件转换为PDF文档.我使用了我在网上找到的以下代码从Picture生成PDF文件,我试图用html文件替换图像,但它失败了.我非常感谢你的帮助.提前致谢,
// Our method to create a PDF file natively on the iPhone
// This method takes two parameters, a CGRect for size and
// a const char, which will be the name of our pdf file
void CreatePDFFile (CGRect pageRect, const char *filename) {
// This code block sets up our PDF Context so that we can draw to it
CGContextRef pdfContext;
CFStringRef path;
CFURLRef url;
CFMutableDictionaryRef myDictionary = NULL;
// Create a CFString from the filename we provide …Run Code Online (Sandbox Code Playgroud) ios ×8
objective-c ×6
swift ×6
iphone ×5
cocoa-touch ×3
screenshot ×3
avplayer ×2
pdf ×2
uiimage ×2
avfoundation ×1
performance ×1
skview ×1
sprite-kit ×1
testing ×1
uicontrol ×1
uitableview ×1
uiview ×1