2012-08-31 11 views
12

私はiOSで2年間、Macでは決してプログラミングしていません。私は自分のiOS開発で持っているシンプルなイメージニーズを処理するための小さなユーティリティーに取り組んでいます。とにかく、私は完璧に動作するiOSのコードを作業しているが、私は全く同等のものがMacのために何も考えていない。iOSからMac GraphicContext説明/変換

私はさまざまなことを試みましたが、実際には "drawRect:"メソッドの外でMac上でグラフィックスコンテキストを開始する方法を理解できません。 iPhoneでは、私はUIGraphicsBeghinImageContext()を使用します。私は他のポストがlockFocus/unlockFocusを使用すると言っていることを知っていますが、私のニーズにどのように正確にその仕事をするかはわかりません。ああ、私は本当にUIImageの "CGImage"プロパティが欠けています。私はNSImageがなぜそれを持つことができないのか理解していませんが、ちょうどそれより少し難解です。

UIImage *mask = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Mask_Image.jpg" ofType:nil]]; 
    UIImage *image = [UIImage imageNamed::@"Test_Image1.jpg"]; 
    UIGraphicsBeginImageContextWithOptions(mask.size, NO, [[UIScreen mainScreen]scale]); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextTranslateCTM(ctx, 0.0, mask.size.height); 
    CGContextScaleCTM(ctx, 1.f, -1.f); 
    [image drawInRect:CGRectMake(0.f, -mask.size.height, image.size.width, image.size.height)]; 
    UIImage *flippedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    CGImageRef maskRef = mask.CGImage; 
    CGImageRef maskCreate = CGImageMaskCreate(CGImageGetWidth(maskRef), 
               CGImageGetHeight(maskRef), 
               CGImageGetBitsPerComponent(maskRef), 
               CGImageGetBitsPerPixel(maskRef), 
               CGImageGetBytesPerRow(maskRef), 
               CGImageGetDataProvider(maskRef), NULL, false); 
    CGImageRef masked = CGImageCreateWithMask([flippedImage CGImage], maskCreate); 

    CGImageRelease(maskCreate); 

    UIImage *maskedImage = [UIImage imageWithCGImage:masked]; 
    CGImageRelease(masked); 

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(image.size.width, image.size.height + (image.size.height * .5)), NO, [[UIScreen mainScreen]scale]); 

    [image drawInRect:CGRectMake(0,0, image.size.width, image.size.height)]; 
    [maskedImage drawInRect:CGRectMake(0, image.size.height, maskedImage.size.width, maskedImage.size.height)]; 

    UIImage *anotherImage = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

//do something with anotherImage 

任意の提案をMac上で(単に)これを達成するために:ここで

はそれだけでマスクから反射された画像を作成し、それらを一緒に組み合わせIOS-基本的に私の作業コードのですか?

+1

のSwift3版:グラフィックスコンテキストを開始し '(CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]' – CodaFi

+0

していますか?そうであれば、どうやってイメージをイメージに描画して取得するのですか(UIGraphicsGetImageFromCurrentImageContext()など)。また、どのようにNSImageとCGImageの間に行くのですか? – daveMac

+1

スウィフトバージョンを追加しました:http://stackoverflow.com/a/34361216/1226095 –

答えて

19

ここでの主な違いは、それはあなたの最初のOS X上で

NSSize size = NSMakeSize(50, 50); 

NSImage* im = [[NSImage alloc] initWithSize:size]; 
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] 
          initWithBitmapDataPlanes:NULL 
              pixelsWide:size.width 
              pixelsHigh:size.height 
             bitsPerSample:8 
            samplesPerPixel:4 
              hasAlpha:YES 
              isPlanar:NO 
             colorSpaceName:NSCalibratedRGBColorSpace 
             bytesPerRow:0 
             bitsPerPixel:0]; 

[im addRepresentation:rep]; 

[im lockFocus]; 

CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort]; 
CGContextClearRect(ctx, NSMakeRect(0, 0, size.width, size.height)); 
CGContextSetFillColorWithColor(ctx, [[NSColor blueColor] CGColor]); 
CGContextFillEllipseInRect(ctx, NSMakeRect(0, 0, size.width, size.height)); 

[im unlockFocus]; 

[[im TIFFRepresentation] writeToFile:@"/Users/USERNAME/Desktop/foo.tiff" atomically:NO]; 

(私はこの例ではARCを使用している、味に保持/リリースを追加)NSImageではに青い円を描く簡単な例ですイメージを作成しなければならない場合、イメージを描画することができます。 iOS上でコンテキストを作成し、そこから画像を抽出します。

基本的に、lockFocusは現在のコンテキストをイメージにして、その上に直接描画してイメージを使用します。

これがあなたの質問のすべてに答えるかどうかは完全にはわかりませんが、少なくともその一部だと思います。

+0

私は、フォーカスをロックするものが何かを考えていましたが、イメージを反転させるようなやり方はどうしたらいいですか?私は文脈を反転し、その中にイメージを描画しようとしましたが、イメージは常に右に出てきます。もう1つのことは、TIFFだけを私が保存できる形式ですか? – daveMac

+0

Swiftバージョンを追加しました:http://stackoverflow.com/a/34361216/1226095 –

9

さて、ここでUIGraphicsBeginImageContextWithOptionsにノートです:

UIGraphicsBeginImageContextWithOptions

指定されたオプションでビットマップベースのグラフィックスコンテキストを作成します。

IOSでも利用可能である(そしてUIGraphicsBeginImageContextWithOptionsはおそらくのラッパーである)OS Xの当量は、CGBitmapContextCreateある:それはCですが

CGContextRef CGBitmapContextCreate (
    void *data, 
    size_t width, 
    size_t height, 
    size_t bitsPerComponent, 
    size_t bytesPerRow, 
    CGColorSpaceRef colorspace, 
    CGBitmapInfo bitmapInfo 
); 

:として宣言

APIでは、CGBitmapContextをCGContextのサブクラスと考えることができます。これはピクセルバッファにレンダリングされますが、CGContextは抽象的なデスティネーションにレンダリングされます。

UIGraphicsGetImageFromCurrentImageContextの場合はCGBitmapContextCreateImageを使用し、ビットマップコンテキストを渡してCGImageを作成できます。

7

ここはスイフト(2.1/10。11 API準拠)Cobbal's answer

let size = NSMakeSize(50, 50); 
    let im = NSImage.init(size: size) 

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

    im.addRepresentation(rep!) 
    im.lockFocus() 

    let rect = NSMakeRect(0, 0, size.width, size.height) 
    let ctx = NSGraphicsContext.currentContext()?.CGContext 
    CGContextClearRect(ctx, rect) 
    CGContextSetFillColorWithColor(ctx, NSColor.blackColor().CGColor) 
    CGContextFillRect(ctx, rect) 

    im.unlockFocus() 
のバージョン
0

Cobbal's answer

シンプル
let size = NSMakeSize(50, 50); 
    let im = NSImage.init(size: size) 

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


    im.addRepresentation(rep!) 
    im.lockFocus() 

    let rect = NSMakeRect(0, 0, size.width, size.height) 
    let ctx = NSGraphicsContext.current()?.cgContext 
    ctx!.clear(rect) 
    ctx!.setFillColor(NSColor.black.cgColor) 
    ctx!.fill(rect) 

    im.unlockFocus()