Schlagwort-Archive: nsimage

ASObjC: NSImage – open, crop and save image

--Cocoa crop
set srcImage to NSImage's alloc()'s initWithContentsOfFile_(picPath)
srcView's setImage_(srcImage)

set w to _width's stringValue() as string as number
set h to _height's stringValue() as string as number
set t to _top's stringValue() as string as number
set l to _left's stringValue() as string as number

set destImage to NSImage's alloc()'s initWithSize_({|width|:w, height:h})

set destRect to {|size|:{w, h}, origin:{0, 0}}
set srcRect to {|size|:{w, h}, origin:{l, t}}

destImage's lockFocus()
srcImage's drawInRect_fromRect_operation_fraction_(destRect, srcRect, current application's NSCompositeSourceOver, 1.0)
destImage's unlockFocus()

destView's setImage_(destImage)

--save as png
set theData to destImage's TIFFRepresentation()
set myNsBitmapImageRepObj to NSBitmapImageRep's imageRepWithData_(theData)
set myNewImageData to (myNsBitmapImageRepObj's representationUsingType_properties_(current application's NSPNGFileType, missing value))
if not (myNewImageData's writeToFile_atomically_(picPath, true)) as boolean then
    set messageText to "There was an error writing to file"
    display dialog messageText buttons {"Ok"}
end if