AppleScriptObjC中的NSImage版本

时间:2017-04-01 02:33:37

标签: applescript nsimage nsimageview applescript-objc

我有一个用于处理照片的小型AppleScriptObjC应用程序。我需要修复我发现的内存泄漏,这似乎是由于照片被缓存而未被释放造成的。每次处理时都会发生这种情况(initWithContentsOfFile_),这会导致每个进程的内存使用量增加。我的代码的相关部分是:

on applicationWillFinishLaunching_(aNotification)
    set inputPath to (choose folder with prompt "Select The Input Directory" default location (path to desktop folder))
    tell application "finder"
    set imageNames to get name of files of inputPath
    end tell
    set imagesRemaining to (number of items of imageNames) as string
    my errorAlert(imagesRemaining & " photos remaining.")
    if (number of items of imageNames) = 0
    my errorAlert("There are no items in the directory.")
    else
    set numberOfImages to number of items of imageNames
    set currentPath to inputPath & (item 1 of imageNames) as string
    set theImage to initWithContentsOfFile_(POSIX path of currentPath) of alloc() of class "NSImage" of current application
    setImage_(theImage) of imageView
    set theImage to missing value
end if
end applicationWillFinishLaunching_

脚本的这一部分分配第一张照片,其他处理功能几乎完全相同。

我要么想弄清楚如何正确地释放每张照片,要么使用setCacheMode:NSImageCacheNever来禁用缓存功能。尽管尝试了很长时间才能找到ApplescriptObjC中的正确语法,但我似乎无法修复内存泄漏。谢谢!

作为参考,这里的按钮动作会导致每次执行时内存增加:

    on photoOutput1_(sender)
        if output1 = ""
        set output1 to (choose folder with prompt "Select The Output Path #1" default location (path to desktop folder))
        else
    end if
tell application "Finder"
set ProcessedPhoto to quoted form of (POSIX path of CurrentPath)
set photoNumber to photoNumber + 1
set DateAppend to do shell script "date +%m-%d_%H.%M"
set DateAppend to DateAppend & "_" & photoNumber
set OutputPath to quoted form of ((POSIX path of output1) & DateAppend & ".jpg")
try
    do shell script "mv -n " & ProcessedPhoto & " " & OutputPath
    on error
    my errorAlert("Unable to move the photo. Please try again.")
    return
end try
            set imageNames to get name of files of inputPath
        end tell
set imagesRemaining to (number of items of imageNames) as string
my errorAlert(imagesRemaining & " photos remaining.")
        if (number of items of imageNames) = 0
        imageView's setImage_(missing value)
        my errorAlert("There are no more photos in the directory.")
        else
        set numberOfImages to number of items of imageNames
        set currentPath to inputPath & (item 1 of imageNames) as string
set theImage to missing value
imageView's setImage_(theImage)
set theImage to initWithContentsOfFile_(POSIX path of currentPath) of alloc() of class "NSImage" of current application
        setImage_(theImage) of imageView
tell application "Finder"
        set OutputImageNames to number of files in folder output1
        end tell
        buttonA's setTitle_("A (" & OutputImageNames & ")")
        end if
    end photoOutput1_

1 个答案:

答案 0 :(得分:1)

您可以尝试同时释放图像变量和图像,因为在Applescript中内容被复制而不是指针:

set theImage to missing value
imageView's setImage_(theImage)

同时检查两次没有其他因素导致您的泄漏。你用过乐器吗?检查分配