gpt4 book ai didi

swift - 使用 cocoa/osx/swift 更改图像的分辨率和大小(无移动应用程序)

转载 作者:行者123 更新时间:2023-11-30 13:38:33 26 4
gpt4 key购买 nike

我尝试以编程方式更改图像的大小和分辨率,然后保存该图像。

imageView 中的图像大小正在变化,但是当我查看文件“file3.png”时,它始终具有 640x1142 的原始分辨率。

我用谷歌搜索但找不到解决方案。我尝试重新绘制图像。但也许这是错误的策略。

谢谢

@IBAction func pickOneImageBtn(sender: AnyObject) {


//load image from path
pickedImage.image = loadImageFromPath(fileInDocumentsDirectory("Angebote.png"))


let newSize = NSSize(width: 10, height: 10)


if let image = pickedImage.image {

print("found image")


//cast to CGImage
var imageRect:CGRect = CGRectMake(0, 0, image.size.width, image.size.height)
let imageRef = image.CGImageForProposedRect(&imageRect, context: nil, hints: nil)

if let imageRefExists = imageRef {
print("Cast to CGImage worked \(imageRefExists)")
}

//redraw to NSImage with new size
let imageWithNewSize = NSImage(CGImage: imageRef!, size: newSize)


//save on disk
let imgData: NSData! = imageWithNewSize.TIFFRepresentation!
let bitmap: NSBitmapImageRep! = NSBitmapImageRep(data: imgData!)
if let pngCoverImage = bitmap!.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [:]) {
pngCoverImage.writeToFile("/...correctpath.../imageSourceForResize/file3.png", atomically: false)
print("saved new image")
}

//the size is smaller
pickedImage.image = imageWithNewSize
}

}

最佳答案

改变

let imgData: NSData! = pickedImage.image!.TIFFRepresentation!

let imgData: NSData! = imageWithNewSize.TIFFRepresentation!

关于swift - 使用 cocoa/osx/swift 更改图像的分辨率和大小(无移动应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35831389/

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