ios - View 中的图像大于设备屏幕

标签 ios iphone swift uiview

我有一个在 Storyboard中创建的 UIView,我想用它来打印表单,我已经实例化了它,然后我调用 View ,然后一切就开始工作了,但是,当这种情况发生在较小的屏幕上时元素,例如 iPhone,它会在屏幕所在的位置将其切断。我不知道需要在哪里调整大小或什么才能避免这种情况。现在,这就是我用来扩大规模的方法,因此我具有高质量(这个问题是在我添加质量规模之前发生的)。我应该在实例化它时设置大小吗?

    func generatePDF() {
    let pdfData = NSMutableData()
    let bounds = self.view.bounds

    UIGraphicsBeginPDFContextToData(pdfData, bounds, nil)

    UIGraphicsBeginPDFPage()

    guard let pdfContext = UIGraphicsGetCurrentContext() else { return }

    let rescale : CGFloat = 4

    func scaler(v: UIView) {
        if !v.isKindOfClass(UIStackView.self) {
            v.contentScaleFactor = 8
        }
        for sv in v.subviews {
            scaler(sv)
        }
    }

    scaler(pdfView)

    let bigSize = CGSize(width: pdfView.frame.size.width*rescale, height: pdfView.frame.size.width*rescale)
    UIGraphicsBeginImageContextWithOptions(bigSize, true, 1)
    let context = UIGraphicsGetCurrentContext()!

    CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
    CGContextFillRect(context, CGRect(origin: CGPoint(x: 0, y: 0), size: bigSize))

    CGContextScaleCTM(context, rescale, rescale)

    pdfView.layer.renderInContext(context)

    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    CGContextSaveGState(pdfContext)
    CGContextTranslateCTM(pdfContext, pdfView.frame.origin.x, pdfView.frame.origin.y)
    CGContextScaleCTM(pdfContext, 1/rescale, 1/rescale)

    let frame = CGRect(origin: CGPoint(x: 0, y: 0), size: bigSize)
    image!.drawInRect(frame)

    CGContextRestoreGState(pdfContext)

    let info : UIPrintInfo = UIPrintInfo(dictionary: nil)
    info.orientation = UIPrintInfoOrientation.Portrait
    info.outputType = UIPrintInfoOutputType.Grayscale
    info.jobName = "Work Order"

    if NSUserDefaults.standardUserDefaults().URLForKey("printer") != nil {
        let printer = UIPrinter(URL: NSUserDefaults.standardUserDefaults().URLForKey("printer")!)
        printer.contactPrinter { (available) in
            if available {

                let printInteraction = UIPrintInteractionController.sharedPrintController()

                printInteraction.printingItem = image
                printInteraction.printInfo = info

                printInteraction.printToPrinter(printer, completionHandler: { (printerController, completed, error) in
                    if completed {
                        let alert = UIAlertController(title: "Printed!", message: nil, preferredStyle: .Alert)
                        self.presentViewController(alert, animated: true, completion: nil)
                        let delay = 1.0 * Double(NSEC_PER_SEC)
                        let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
                        dispatch_after(time, dispatch_get_main_queue(), {
                            alert.dismissViewControllerAnimated(true, completion: {
                                self.dismissViewControllerAnimated(false, completion: nil)
                            })
                        })
                    }
                })

            }
        }
    } else {
        self.dismissViewControllerAnimated(false, completion: nil)
        let alert = UIAlertController(title: "Select Printer", message: "\nPlease select a printer in the More section and then try your print again", preferredStyle: .Alert)
        let okayButton = UIAlertAction(title: "Okay", style: .Default, handler: nil)
        alert.addAction(okayButton)
        UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
    }
}

最佳答案

尝试将 UIView 的边界设置为屏幕的边界,如下所示:

let screenSize: CGRect = UIScreen.main.bounds
view.frame = CGRect(x: 0.0, y: 0.0, width: screenSize.width, height: screenSize.height)

关于ios - View 中的图像大于设备屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40091789/

相关文章:

android - 对 'cocos2d::UserDefault' 的 undefined reference

iphone - 如果没有声明 nsautoreleasepool,autorelease 调用是否会崩溃?

iphone - 基于iOS 4并部署iOS 3的媒体播放器问题

ios - 跨多个上下文重用 NSFetchRequest 时崩溃

ios - 流布局不支持负尺寸或零尺寸

ios - 为什么github上的某些ios项目仍然使用IBOutlet而不使用Interface builder文件?

swift - sprite.xScale 响应速度慢

ios - "Could not cast value of type ' __NSCFString ' (0x103c93c50) to ' NSNumber ' (0x103535b88)"错误

ios - Swift decodable 无法访问数组中的嵌套数据

ios - 单击搜索栏不会提示键盘(iOS Swift)