Imageview到InputStream

时间:2018-09-16 23:08:49

标签: android kotlin

我需要尝试将imageView的图像转换为要发送到OutPutFile的帮助,但是在发送之前需要先读入。

此代码使用的是静态图片。取而代之的是,我想使用imageView作为我发送的图片。

func window(_ window: NSWindow, shouldDragDocumentWith event: NSEvent, from dragImageLocation: NSPoint, with pasteboard: NSPasteboard) -> Bool {

    if let dragImage = self.snapshot {
        //  Replace string with our URL string
        let urlString = String(format: """
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
<key>URL</key>
<string>%@</string>
</dict>
</plist>
""", ((document as! Document).fileURL?.absoluteString)!)
        pasteboard.setString(urlString, forType: NSPasteboardTypeString)

        window.drag(dragImage, at: dragImageLocation, offset: NSZeroSize, event: event, pasteboard: pasteboard, source: self, slideBack: true)
    }

    return false
}

到目前为止,我已经使Bitmap可以使用此代码字符串,但是我仍然停留在将图像文件发送到有意义的地方的部分

        val imageFileName: String = globals.sampN.toString() + "-" + globals.dayNo.toString() + "-" + globals.pictureCount.toString() + ".jpg"
        val storageDir: File = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        if(!storageDir.exists()) storageDir.mkdirs()

        val imageFile = File(storageDir, imageFileName)


        val myOutput:OutputStream = FileOutputStream(imageFile)
        val buffer = ByteArray(1024)
        val myInput = getAssets().open("NoPhoto.jpg") // <- This is the place where it needs
        var length: Int = myInput.read(buffer)        // to be replaced with imageView
        while (length > 0) {
            myOutput.write(buffer, 0, length)
            length = myInput.read(buffer)
        }
        myInput.close()
        myOutput.flush()
        myOutput.close()
        mailBitmap = setScaledBitmap(imageFile.absolutePath)
        println("mailBitmap " + mailBitmap.toString())

0 个答案:

没有答案