2012-03-20 19 views
1

我試圖將as3delaunay library移植到Obj-C(爲了好玩並學習更多的Obj-C)。它很好,但我真的不明白如何將BitmapData的使用轉換爲Cocoa。這裏有幾個前述圖書館的相關部分:Actionscript to Obj-C/Cocoa:如何表示BitmapData?

Edge.as

internal function makeDelaunayLineBmp():BitmapData 
    { 
     var p0:Point = leftSite.coord; 
     var p1:Point = rightSite.coord; 

     GRAPHICS.clear(); 
     // clear() resets line style back to undefined! 
     GRAPHICS.lineStyle(0, 0, 1.0, false, LineScaleMode.NONE, CapsStyle.NONE); 
     GRAPHICS.moveTo(p0.x, p0.y); 
     GRAPHICS.lineTo(p1.x, p1.y); 

     var w:int = int(Math.ceil(Math.max(p0.x, p1.x))); 
     if (w < 1) 
     { 
      w = 1; 
     } 
     var h:int = int(Math.ceil(Math.max(p0.y, p1.y))); 
     if (h < 1) 
     { 
      h = 1; 
     } 
     var bmp:BitmapData = new BitmapData(w, h, true, 0); 
     bmp.draw(LINESPRITE); 
     return bmp; 
    } 

這就是所謂的在下面的函數,在selectNonIntersectingEdges.as

internal function selectNonIntersectingEdges(keepOutMask:BitmapData, edgesToTest:Vector.<Edge>):Vector.<Edge> 
{ 
    if (keepOutMask == null) 
    { 
     return edgesToTest; 
    } 

    var zeroPoint:Point = new Point(); 
    return edgesToTest.filter(myTest); 

    function myTest(edge:Edge, index:int, vector:Vector.<Edge>):Boolean 
    { 
     var delaunayLineBmp:BitmapData = edge.makeDelaunayLineBmp(); 
     var notIntersecting:Boolean = !(keepOutMask.hitTest(zeroPoint, 1, delaunayLineBmp, zeroPoint, 1)); 
     delaunayLineBmp.dispose(); 
     return notIntersecting; 
    } 
} 

這似乎也作爲參數在SiteList.as

public function nearestSitePoint(proximityMap:BitmapData, x:Number, y:Number):Point 
    { 
     var index:uint = proximityMap.getPixel(x, y); 
     if (index > _sites.length - 1) 
     { 
      return null; 
     } 
     return _sites[index].coord; 
    } 

在Obj-C/Cocoa中代表這種行爲和/或使用BitmapData的好方法是什麼?

+0

在'makeDelaunayLineBmp()'函數中,什麼是'LINESPRITE'? – 2012-03-24 05:31:52

回答

1

的核心圖形庫具有非常強大的圖形處理能力 - 儘管這一切都基於C語言。您可以創建一個8位RGBA(紅,綠,藍,alpha)位圖背景是這樣的:

size_t const bytesPerRow = 4 * width; 
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); 
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast; 
CGContextRef ctx = CGBitmapContextCreate(NULL, width, height, 8, bytesPerRow, colorspace, bitmapInfo); 
CFRelease(colorspace); 

這相當於什麼AS3的BitmapData了。

ctx現在引用了您最終必須發佈的位圖上下文,其格式爲CGContextRelease(ctx)

您可以操縱上下文,即將其與各種CGContext*函數一起使用。如果您最終需要將其另存爲圖像(例如JPEG數據),請使用CGBitmapContextCreateImage()

+0

謝謝!這會讓我朝着正確的方向前進。 – Clay 2012-03-29 00:34:50

相關問題