02.12.2012 22:25:00 • Categories: ASObjC • Tags: Asobjc, 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_operationfraction(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 representationUsingTypeproperties(current application's NSPNGFileType, missing value)) if not (myNewImageData's writeToFileatomically(picPath, true)) as boolean then set messageText to "There was an error writing to file" display dialog messageText buttons {"Ok"} end if