gpt4 book ai didi

ios - OSX UIGraphicsBeginImageContext

转载 作者:行者123 更新时间:2023-11-29 10:31:39 27 4
gpt4 key购买 nike

我现在正在为 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com