2017-10-16 66 views
0

我有以下目標C代碼將文本寫入Image.I'm是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

對不起,#2是不是代碼轉換服務。代碼非常簡單。在代碼完成的幫助下(還有一點閱讀文檔),它很容易轉換。 – vadian

+0

@vadian okay ...... :( – techno

+0

你使用的是哪個swift版本? – 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(string:'expect a URL string包括'file://'方案,對於文件系統URL,你必須使用'URL(fileURLWithPath:'。順便說一下:'NSImage'有API來直接從URL獲取圖像。 – vadian

+0

我很確定在OP的問題中提到的URL是一個虛擬URL,這一點在命名約定中很明顯 – Malik

+0

ObjC代碼'dataWithContentsOfFile'明確暗示了本地文件系統中的一個文件 – vadian