作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个问题苹果尚未接受我的应用程序,因为用户无法自由地将图像保存在他选择的 Mac 上
该应用程序适用于 Mac,代码采用 Swift 2.3
var m_strFloderPath:String = ""
var m_imgLogo:NSImage!
@IBOutlet var m_imgAdd: NSImageView!
@IBOutlet var m_btiOS: NSButton!
@IBOutlet var m_btAndroid: NSButton!
@IBOutlet var m_vDropView: DropView!
// MARK:Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
initView()
}
func initView() {
print("scale:\(NSScreen.mainScreen()?.backingScaleFactor)")
// init DropView
self.m_vDropView.initDrop();
self.m_vDropView.getimageblock = {(image:NSImage!) -> Void in
print("get new image");
self.didReceiveNewImageLogo(image)
}
// init enabled status.
self.m_btiOS.enabled = false
self.m_btAndroid.enabled = false
// for load image from menubar.
let delegate:AppDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
delegate.completeLoadImage = {
(image) -> Void in
self.didReceiveNewImageLogo(image)
}
}
// MARK: Privete Method
override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject?) {
let segueId:String = segue.identifier!
let dvc:DialogViewController = segue.destinationController as! DialogViewController
dvc.m_strFolderName = ""
func doMakeIcon(){
var aryDatalist:Array<CGFloat> = []
// iOS
if segueId == self.kiOS {
print("iOS")
aryDatalist = [29.0,58.0,87.0,40.0,80.0,120.0,60.0,180.0]
}
// Android
else if segueId == self.kAndroid {
print("Android")
aryDatalist = [36.0,48.0,64.0,72.0,96.0,144.0,192.0,512.0]
}
for size in aryDatalist {
self.saveImage(size)
}
JNUtility.showOneButtonAlertWithTitle("ICON Build Success", message: "Your icons's folder now is on the desktop") { (Void) -> Void in
}
}
dvc.handleConfirmClosure = {
(strFolderName) -> Void in
// Create Folder
if self.createFolder(strFolderName) == true {
doMakeIcon()
}
else {
JNUtility.showAlertWithTitle("Warning", message: "\"\(strFolderName)\" ,this folder name already exist, do you want to replace it ?", completion: {
doMakeIcon()
})
}
}
}
func didReceiveNewImageLogo(image:NSImage){
self.m_imgLogo = image
self.m_imgAdd.hidden = true;
self.m_btiOS.enabled = true;
self.m_btAndroid.enabled = true;
}
func resize(image: NSImage, w: CGFloat, h: CGFloat) -> NSImage {
let destSize = NSMakeSize(w, h)
let newImage = NSImage(size: destSize)
newImage.lockFocus() // make the drawing appear on the newImage instead of on the screen
image.drawInRect(NSMakeRect(0, 0, destSize.width, destSize.height), fromRect: NSMakeRect(0, 0, image.size.width, image.size.height), operation: NSCompositingOperation.SourceOver, fraction: CGFloat(1))
newImage.unlockFocus() // unlock after finished drawing.
return NSImage(data: newImage.TIFFRepresentation!)!
}
func saveImage(size:CGFloat) {
let scale :CGFloat = NSScreen.mainScreen()!.backingScaleFactor
let image = resize(self.m_imgLogo!, w: size/scale, h: size/scale)
let strImgName = "icon_\(size.string(0))x\(size.string(0))"
let filePathToWrite = "\(self.m_strFloderPath)/\(strImgName).png"
var imageData = image.TIFFRepresentation
let imageRep = NSBitmapImageRep(data: imageData!)
let imageProps = [NSImageCompressionFactor:NSNumber(float: 1.0)]
imageData = imageRep!.representationUsingType(NSBitmapImageFileType.PNG, properties: imageProps)
imageData?.writeToFile(filePathToWrite, atomically: false)
}
func createFolder(strFolderName:String) ->Bool {
let manager = NSFileManager.defaultManager()
let paths = NSSearchPathForDirectoriesInDomains(.DesktopDirectory, .UserDomainMask, true)[0] as String
m_strFloderPath = "\(paths)/\(strFolderName)"
do {
if manager.isReadableFileAtPath(m_strFloderPath) {
print("already exist")
return false
}
try manager.createDirectoryAtPath(m_strFloderPath, withIntermediateDirectories: true, attributes: nil)
return true
} catch {
print("error")
return false
}
}
}
最佳答案
import Cocoa
class ViewController: NSViewController {
let kiOS:String = "iOS"
let kAndroid:String = "Android"
var m_strFloderPath:String = ""
var m_imgLogo:NSImage!
@IBOutlet var m_imgAdd: NSImageView!
@IBOutlet var m_btiOS: NSButton!
@IBOutlet var m_btAndroid: NSButton!
@IBOutlet var m_vDropView: DropView!
override func viewDidLoad() {
super.viewDidLoad()
initView()
}
func initView() {
print("scale:\(NSScreen.mainScreen()?.backingScaleFactor)")
// init DropView
self.m_vDropView.initDrop();
self.m_vDropView.getimageblock = {(image:NSImage!) -> Void in
print("get new image");
self.didReceiveNewImageLogo(image)
}
// init enabled status.
self.m_btiOS.enabled = false
self.m_btAndroid.enabled = false
// for load image from menubar.
let delegate:AppDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
delegate.completeLoadImage = {
(image) -> Void in
self.didReceiveNewImageLogo(image)
}
}
// MARK: Privete Method
override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject?) {
let segueId:String = segue.identifier!
let dvc:DialogViewController = segue.destinationController as! DialogViewController
dvc.m_strFolderName = ""
func doMakeIcon(){
var aryDatalist:Array<CGFloat> = []
// iOS
if segueId == self.kiOS {
print("iOS")
aryDatalist = [29.0,58.0,87.0,40.0,80.0,120.0,60.0,180.0]
}
// Android
else if segueId == self.kAndroid {
print("Android")
aryDatalist = [36.0,48.0,64.0,72.0,96.0,144.0,192.0,512.0]
}
for size in aryDatalist {
self.saveImage(size)
}
JNUtility.showOneButtonAlertWithTitle("ICON Build Success", message: "Your icons's folder now is on the desktop") { (Void) -> Void in
}
}
dvc.handleConfirmClosure = {
(strFolderName) -> Void in
// Create Folder
if self.createFolder(strFolderName) == true {
doMakeIcon()
}
else {
JNUtility.showAlertWithTitle("Warning", message: "\"\(strFolderName)\" ,this folder name already exist, do you want to replace it ?", completion: {
doMakeIcon()
})
}
}
}
func didReceiveNewImageLogo(image:NSImage){
self.m_imgLogo = image
self.m_imgAdd.hidden = true;
self.m_btiOS.enabled = true;
self.m_btAndroid.enabled = true;
}
func resize(image: NSImage, w: CGFloat, h: CGFloat) -> NSImage {
let destSize = NSMakeSize(w, h)
let newImage = NSImage(size: destSize)
newImage.lockFocus() // make the drawing appear on the newImage instead of on the screen
image.drawInRect(NSMakeRect(0, 0, destSize.width, destSize.height), fromRect: NSMakeRect(0, 0, image.size.width, image.size.height), operation: NSCompositingOperation.SourceOver, fraction: CGFloat(1))
newImage.unlockFocus() // unlock after finished drawing.
return NSImage(data: newImage.TIFFRepresentation!)!
}
func saveImage(size:CGFloat) {
let scale :CGFloat = NSScreen.mainScreen()!.backingScaleFactor
let image = resize(self.m_imgLogo!, w: size/scale, h: size/scale)
let strImgName = "icon_\(size.string(0))x\(size.string(0))"
let filePathToWrite = "\(self.m_strFloderPath)/\(strImgName).png"
var imageData = image.TIFFRepresentation
let imageRep = NSBitmapImageRep(data: imageData!)
let imageProps = [NSImageCompressionFactor:NSNumber(float: 1.0)]
imageData = imageRep!.representationUsingType(NSBitmapImageFileType.PNG, properties: imageProps)
imageData?.writeToFile(filePathToWrite, atomically: false)
}
func createFolder(strFolderName:String) ->Bool {
let manager = NSFileManager.defaultManager()
let paths = NSSearchPathForDirectoriesInDomains(.DesktopDirectory, .UserDomainMask, true)[0] as String
m_strFloderPath = "\(paths)/\(strFolderName)"
do {
if manager.isReadableFileAtPath(m_strFloderPath) {
print("already exist")
return false
}
try manager.createDirectoryAtPath(m_strFloderPath, withIntermediateDirectories: true, attributes: nil)
return true
} catch {
print("error")
return false
}
}
}
所有代码都在这里
关于swift - 如何将图像保存在用户选择的文件夹中,而不是我选择的文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42231806/
我是一名优秀的程序员,十分优秀!