ios - OSX UIGraphicsBeginImageContext

标签 ios macos nsimage uigraphicscontext uiimagepngrepresentation

我现在正在为 Mac OSX 创建一个 iOS 应用程序。我有下面的代码,将图像转换为 1024 的大小,并根据图像的长宽比计算出宽度。这适用于 iOS,但显然不适用于 OSX。我不确定如何创建 NSImage 的 PNG 表示形式,也不知道应该使用什么来代替 UIGraphicsBeginImageContext。有什么建议么? 谢谢。

        var image = myImageView.image

        let imageData = UIImagePNGRepresentation(image)
        let imageWidth = image?.size.width
        let calculationNumber:CGFloat = imageWidth! / 1024.0

        let imageHeight = image?.size.height
        let newImageHeight = imageHeight! / calculationNumber

        UIGraphicsBeginImageContext(CGSizeMake(1024.0, newImageHeight))
        image?.drawInRect(CGRectMake(0, 0, 1024.0, newImageHeight))
        var resizedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        let imageData = UIImagePNGRepresentation(resizedImage)

        let theImageData:NSData = UIImagePNGRepresentation(resizedImage)
        imageFile = PFFile(data: theImageData)

最佳答案

您可以使用:

let image = NSImage(size: newSize)
image.lockFocus()
//draw your stuff here
image.unlockFocus()
if let data = image?.TIFFRepresentation {
    let imageRep = NSBitmapImageRep(data: data)
    let imageData = imageRep?.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [:])
    //do something with your PNG data here
}

关于ios - OSX UIGraphicsBeginImageContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29348487/

相关文章:

c++ - LLD - 未知参数 : -arch

快速调整图像大小并保存分辨率问题

objective-c - 从桌面加载 NSImage 在 cocoa 应用程序中为零

Swift读写图片占用大量内存

ios - 等待操作完成

ios - 关于 Analytics 屏幕报告的 Firebase Analytics 错误

ios - 如何在 iOS 中将图像从资源中分离出来

ios - 简单地显示 UIInterpolatingMotionEffect 的值?

macos - 当我在终端中调用它时,Emacs 错误 "Failed to initialize color list unarchiver"

iphone - 如何创建带有字符串文字的 NSArray?