Last active
February 21, 2016 13:38
-
-
Save gitbricho/289bfc745bf9cc45596e to your computer and use it in GitHub Desktop.
OS X Graphic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
//青色のオブジェクトを作成 | |
let blue = NSColor.blueColor() | |
blue.setFill() | |
//100x100 の4角形を作成 | |
let r = NSRect(x:0, y:0, width:100, height:100) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ビュークラスを定義 | |
class MyView01: NSView { | |
//与えらえた4角形を青色に塗り潰し、ビューフレーム全体に広げる | |
override func drawRect(dirtyRect: NSRect) { | |
let blue = NSColor.blueColor() | |
blue.setFill() | |
NSRectFill(self.bounds) | |
let bp = NSBezierPath(ovalInRect: NSInsetRect(self.bounds, 20,20)) | |
NSColor.yellowColor().setFill() | |
bp.fill() | |
} | |
} | |
//ビューインスタンスを生成:100x100の四角形のビュー | |
let view1a = MyView01(frame: r) | |
let view1b = MyView01(frame: NSRect(x:0, y:0, width:150, height:100)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//赤い円の中に白い正方形を描画 | |
class MyView02: NSView { | |
@IBInspectable var v円塗りつぶし色: NSColor = NSColor.redColor() | |
@IBInspectable var v四角形塗りつぶし色: NSColor = NSColor.whiteColor() | |
override func drawRect(rect: NSRect) { | |
//###### 領域内に円を表示 ######_ | |
//パスを生成(楕円) | |
let l円形パス = NSBezierPath(ovalInRect: rect) | |
//塗りつぶしの色の設定と塗りつぶし | |
v円塗りつぶし色.setFill() | |
l円形パス.fill() | |
//###### 上記の楕円内に四角形を描画する ###### | |
//パスを生成 | |
let l四角形パス = NSBezierPath(rect: | |
iOSMakeRect(rect.width/4, y: rect.height/4, | |
w: rect.width/2, h: rect.height/2, rect: rect)) | |
v四角形塗りつぶし色.set() | |
l四角形パス.fill() | |
} | |
func iOSMakeRect(x:CGFloat, y:CGFloat, w:CGFloat, h:CGFloat, rect: NSRect) -> NSRect { | |
return NSMakeRect(x, rect.height-y, w, -h) | |
} | |
} | |
let view2 = MyView02(frame: NSMakeRect(0,0,200,200)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//曲線を描画 | |
class MyView03: NSView { | |
@IBInspectable var v線の色: NSColor = NSColor.grayColor() | |
@IBInspectable var v塗りつぶし色: NSColor = NSColor.whiteColor() | |
override func drawRect(dirtyRect: NSRect) { | |
let l領域の右上 = iOSCGPoint( | |
bounds.origin.x, | |
y: bounds.origin.y, rect: bounds) | |
let l領域の左上 = iOSCGPoint( | |
bounds.origin.x + bounds.width, | |
y: bounds.origin.y, rect: bounds) | |
let l領域の右下 = iOSCGPoint( | |
bounds.origin.x, | |
y: bounds.origin.y + bounds.height, rect: bounds) | |
let l領域の左下 = iOSCGPoint( | |
bounds.origin.x + bounds.width, | |
y: bounds.origin.y + bounds.height, rect: bounds) | |
let l描画パス = NSBezierPath() | |
l描画パス.lineWidth = 4 | |
let l起点 = l領域の右上 | |
let l終点 = l領域の左下 | |
l描画パス.moveToPoint(l起点) | |
l描画パス.curveToPoint(l終点, | |
controlPoint1: l領域の右下, | |
controlPoint2: l領域の左上 | |
) | |
v塗りつぶし色.setFill() | |
l描画パス.fill() | |
v線の色.setStroke() | |
l描画パス.stroke() | |
} | |
func iOSCGPoint(x:CGFloat, y:CGFloat, rect: NSRect) -> CGPoint { | |
return CGPoint(x: x, y: rect.height - y) | |
} | |
} | |
let view3 = MyView03(frame: NSMakeRect(0,0,200,200)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//グラデーション | |
class MyView04: NSView { | |
@IBInspectable var v開始色: NSColor = NSColor( | |
red: 0.20, green: 0.20, blue: 0.25, alpha: 1.0) | |
@IBInspectable var v終了色: NSColor = NSColor( | |
red: 0.50, green: 0.65, blue: 0.70, alpha: 1.0) | |
override func drawRect(dirtyRect: NSRect) { | |
let lグラデーション: NSGradient = NSGradient( | |
startingColor: v開始色, | |
endingColor: v終了色)! | |
let l描画パス = NSBezierPath(roundedRect: self.bounds, | |
xRadius: 8.0, yRadius: 8.0) | |
lグラデーション.drawInBezierPath(l描画パス, angle: 225) | |
} | |
} | |
let view4 = MyView04(frame: NSMakeRect(0,0,200,200)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//実線、破線などの線を描画 | |
class MyView07: NSView { | |
override func drawRect(dirtyRect: NSRect) { | |
let l起点x:CGFloat = dirtyRect.origin.x + 10 | |
let l終点x:CGFloat = dirtyRect.origin.x + dirtyRect.width - 10 | |
var v描画パス = NSBezierPath() | |
var y:CGFloat = 50 | |
drowLine(v描画パス, startX: l起点x, endX: l終点x, y: y, lineWidth: 5.0) | |
v描画パス = NSBezierPath() | |
y += 50 | |
drowLine(v描画パス, startX: l起点x, endX: l終点x, y: y, lineWidth: 15.0) | |
v描画パス = NSBezierPath() | |
v描画パス.lineCapStyle = NSLineCapStyle.RoundLineCapStyle | |
y += 50 | |
drowLine(v描画パス, startX: l起点x, endX: l終点x, y: y, lineWidth: 15.0) | |
v描画パス = NSBezierPath() | |
v描画パス.setLineDash([30,2,3,2,3,2,30], count: 6, phase: 0) | |
y += 20 | |
drowLine(v描画パス, startX: l起点x, endX: l終点x, y: y, lineWidth: 3.0) | |
v描画パス = NSBezierPath() | |
v描画パス.setLineDash([20,2,3,2,20], count: 4, phase: 0) | |
y += 20 | |
drowLine(v描画パス, startX: l起点x, endX: l終点x, y: y, lineWidth: 3.0) | |
v描画パス = NSBezierPath() | |
v描画パス.setLineDash([20,5], count: 2, phase: 0) | |
y += 20 | |
drowLine(v描画パス, startX: l起点x, endX: l終点x, y: y, lineWidth: 3.0) | |
} | |
func drowLine(path:NSBezierPath, startX:CGFloat, endX:CGFloat, y:CGFloat, | |
lineWidth:CGFloat) { | |
let rect = self.bounds | |
path.moveToPoint(iOSMakePoint(startX, y: y, rect: rect)) | |
path.lineToPoint(iOSMakePoint(endX, y: y, rect: rect)) | |
path.lineWidth = lineWidth | |
path.stroke() | |
} | |
func iOSMakePoint(x:CGFloat, y:CGFloat, rect: NSRect) -> CGPoint { | |
return CGPoint(x: x, y: rect.height - y) | |
} | |
} | |
let view7 = MyView07(frame: NSMakeRect(0,0,200,300)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
import Quartz | |
//プレイグラウンドのリソースとして追加した写真のファイルパスを指定 | |
var myPicPath:NSString = NSBundle.mainBundle().pathForResource("sampleImage1", ofType:"jpg")! | |
//上記ファイルパスで指定されるイメージを作成 | |
var myPic = NSImage(contentsOfFile: myPicPath as String) | |
//イメージに四角形と線を描画する | |
let size = NSMakeSize(300, 300) | |
var img = NSImage(size: size) | |
let blueColor = NSColor.blueColor() | |
img.lockFocus() | |
blueColor.set() | |
NSRectFill(NSMakeRect(20,20,50,100)) | |
var path = NSBezierPath() | |
path.lineWidth = 4.0 | |
path.moveToPoint(NSMakePoint(100,50)) | |
path.lineToPoint(NSMakePoint(200,100)) | |
path.lineToPoint(NSMakePoint(180,200)) | |
path.lineToPoint(NSMakePoint(150,40)) | |
path.stroke() | |
img.unlockFocus() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fファイルへ出力(出力イメージ: NSImage, パス: String, atomically: Bool = true) -> Bool { | |
let lビットマップ = NSBitmapImageRep( | |
bitmapDataPlanes: nil, | |
pixelsWide: Int(出力イメージ.size.width), | |
pixelsHigh: Int(出力イメージ.size.height), | |
bitsPerSample: 8, | |
samplesPerPixel: 4, | |
hasAlpha: true, | |
isPlanar: false, | |
colorSpaceName: NSDeviceRGBColorSpace, | |
bytesPerRow: 0, bitsPerPixel: 0)! | |
lビットマップ.size = 出力イメージ.size | |
NSGraphicsContext.saveGraphicsState() | |
NSGraphicsContext.setCurrentContext(NSGraphicsContext(bitmapImageRep: lビットマップ)) | |
出力イメージ.drawAtPoint(CGPoint.zero, fromRect: NSRect.zero, | |
operation: NSCompositingOperation.CompositeSourceOver, fraction: 1.0) | |
NSGraphicsContext.restoreGraphicsState() | |
if let lPNGデータ = lビットマップ.representationUsingType( | |
NSBitmapImageFileType.NSPNGFileType, properties: [NSImageCompressionFactor: 1.0]) { | |
return lPNGデータ.writeToFile( | |
(パス as NSString).stringByStandardizingPath, atomically: atomically) | |
} else { | |
return false | |
} | |
} | |
print("カレントパス:\(NSFileManager.defaultManager().currentDirectoryPath)") | |
//カレントディレクトリにファイルとしてイメージを出力する | |
fファイルへ出力(myPic!, パス: "myPic.png") | |
fファイルへ出力(img, パス: "RectAndLine.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment