2017-10-16 29 views
0

イメージにテキストを書き込むためのCコードがあります。私はSWIFTを初めて使用しています。これはどのようにSWIFTで行うことができますか?SWIFT-OSXのNSImageにテキストを書き込む

float width = 10.0; 
float height = 10.0; 

NSImage *finalImage = [[NSImage alloc] initWithSize:NSMakeSize(width, height)]; 

// obtain images - your sources may vary 
NSImage *overlay = [[NSImage alloc] initWithData:[NSData dataWithContentsOfFile:@"/path/to/overlay_image.jpg"]]; 
NSImage *mainImage = [[NSImage alloc] initWithData:[NSData dataWithContentsOfFile:@"/path/to/main_image.jpg"]]; 

[finalImage lockFocus]; 

// draw the base image 
[mainImage drawInRect:NSMakeRect(0, 0, width, height) 
         fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0]; 

// draw the overlay image at some offset point 
[overlay drawInRect:NSMakeRect(10, 10, [overlay size].width, [overlay size].height) 
      fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0]; 

[finalImage unlockFocus]; 

NSData *finalData = [finalImage TIFFRepresentation]; 

[[[NSBitmapImageRep imageRepWithData:finalData] representationUsingType:NSJPEGFileType properties:nil] writeToFile:[NSString stringWithFormat:@"/path/to/folder/new_image.jpg"] atomically:YES]; 

UPDATE:私はこの方法を使用する場合

私はそれが私が出てトリミングを取得Image..Butの上に文字列を描画するには、次の方法をしている画像portions..Seemsの何かがそれと間違っています..ご意見をお聞かせください。

func drawText(image :NSImage) ->NSImage 
{ 
    let text = "Sample Text" 
    let font = NSFont.boldSystemFont(ofSize: 18) 
    let imageRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height) 

    let textRect = CGRect(x: 5, y: 5, width: image.size.width - 5, height: image.size.height - 5) 
    let textStyle = NSMutableParagraphStyle.default().mutableCopy() as! NSMutableParagraphStyle 
    let textFontAttributes = [ 
     NSFontAttributeName: font, 
     NSForegroundColorAttributeName: NSColor.white, 
     NSParagraphStyleAttributeName: textStyle 
    ] 

    let im:NSImage = NSImage(size: image.size) 

    let rep:NSBitmapImageRep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(image.size.width), pixelsHigh: Int(image.size.height), bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: NSCalibratedRGBColorSpace, bytesPerRow: 0, bitsPerPixel: 0)! 

    im.addRepresentation(rep) 

    im.lockFocus() 

    image.draw(in: imageRect) 
    text.draw(in: textRect, withAttributes: textFontAttributes) 

    im.unlockFocus() 

    return im 
} 
+2

にあります申し訳ありませんが、StackOverflowのはコード変換サービスではありません。コードはかなり簡単です。コード補完(およびドキュメントを少し読んで)の助けを借りて、それは変換するのがとても簡単です。 – vadian

+0

@vadian大丈夫......( – techno

+0

速いバージョンをお使いですか? – Malik

答えて

1

ここで、このコードはスウィフト3

let width: CGFloat = 10.0 
let height: CGFloat = 10.0 

let finalImage = NSImage(size: NSMakeSize(width, height)) 

// obtain images - your sources may vary 
var overlay: NSImage? 
var mainImage: NSImage? 

if let url = URL(string: "/path/to/overlay_image.jpg") { 
    do { 
     let data = try Data(contentsOf: url) 
     overlay = NSImage(data: data) 
    } catch { 
     print("Unable to get data") 
    } 
} 

if let url = URL(string: "/path/to/main_image.jpg") { 
    do { 
     let data = try Data(contentsOf: url) 
     mainImage = NSImage(data: data) 
    } catch { 
     print("Unable to get data") 
    } 
} 

finalImage.lockFocus() 

// draw the base image 
mainImage?.draw(in: NSMakeRect(0, 0, width, height), from: NSZeroRect, operation: NSCompositingOperation.sourceOver, fraction: 1.0) 

// draw the overlay image at some offset point 
if overlay != nil { 
    overlay?.draw(in: NSMakeRect(10, 10, overlay!.size.width, overlay!.size.height), from: NSZeroRect, operation: NSCompositingOperation.sourceOver, fraction: 1.0) 
} 

finalImage.unlockFocus() 

if let finalData = finalImage.tiffRepresentation, let url = URL(string: "/path/to/folder/new_image.jpg") { 
    do { 
     try NSBitmapImageRep(data: finalData)?.representation(using: NSJPEGFileType, properties: [:])?.write(to: url) 
    } catch { 
     print("Failed to write") 
    } 
} 
+0

'URL(文字列:' URL文字列が必要です。 'file://'スキームを含むファイルシステムのURLについては 'URL(fileURLWithPath:'。)を使用しなければなりません: 'NSImage'はURLから直接画像を取得するAPIを持っています – vadian

+0

OPの質問に記載されているURLは、命名規則で明白なダミーのURLです。 – Malik

+0

ObjCコードの 'dataWithContentsOfFile'は、ローカルファイルシステムのファイルを明示しています。 – vadian

関連する問題