Objective C 在GCD中使用dispatch_once

dispatch_queue_t get_my_background_queue()
{
	static dispatch_once_t once;
	static dispatch_queue_t my_queue;
	dispatch_once(&once, ^{
		my_queue = dispatch_queue_create("com.example.background", NULL);
	});
	return my_queue;
}

Objective C Evento Touch conposicioÌ?? n

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];
	NSLog(@"Position of touch: %.3f, %.3f", pos.x, pos.y);
	
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];
	NSLog(@"Position of touch: %.3f, %.3f", pos.x, pos.y);
	
}

Objective C 在GCD中使用dispatch_once

dispatch_queue_t get_my_background_queue()
{
	static dispatch_once_t once;
	static dispatch_queue_t my_queue;
	dispatch_once(&once, ^{
		my_queue = dispatch_queue_create("com.example.background", NULL);
	});
	return my_queue;
}

Objective C 在细胞界限内绘制标准聚焦环

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView;
{
    // other stuff might happen here
    if ([self showsFirstResponder]) {
         // showsFirstResponder is set for us by the NSControl that is drawing  us.
        NSRect focusRingFrame = cellFrame;
        focusRingFrame.size.height -= 2.0f;
        [NSGraphicsContextsaveGraphicsState];
        NSSetFocusRingStyle(NSFocusRingOnly);
        [[NSBezierPath bezierPathWithRect: NSInsetRect(focusRingFrame, 4.0f, 4.0f)] fill];
        [NSGraphicsContext restoreGraphicsState];
    }
    // other stuff might happen here
}

Objective C 添加到OS X状态托盘

@interface Tray : NSObject <NSApplicationDelegate> {
	NSStatusItem *trayItem;
}
@end

@implementation Tray

- (IBAction)testAction:(id)sender;
{
	NSLog(@"Hello World");
}

- (IBAction)quitAction:(id)sender;
{
	[NSApp terminate:sender];
}

- (void)applicationDidFinishLaunching:(NSNotification *)note;
{
	NSZone *zone = [NSMenu menuZone];
	NSMenu *menu = [[[NSMenu allocWithZone:zone] init] autorelease];
	NSMenuItem *item;
	
	item = [menu addItemWithTitle:@"Testing" action:@selector(testAction:) keyEquivalent:@""];
	[item setTarget:self];
	
	item = [menu addItemWithTitle:@"Quit" action:@selector(quitAction:) keyEquivalent:@""];
	[item setTarget:self];
	
	trayItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
	[trayItem setMenu:menu];
	[trayItem setHighlightMode:YES];
	[trayItem setTitle:@"HERE"];
}

- (void)dealloc;
{
	[trayItem release];
	[super dealloc];
}

@end

Objective C init cocos2d的contentScale

// Obtain the shared director in order to...
CCDirector *director = [CCDirector sharedDirector];
	
// Enables High Res mode
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
	[director setContentScaleFactor:[[UIScreen mainScreen] scale]];
}

Objective C IplImage到NSImage

/* IplImage to NSImage
  * Kelly Breed
*/
#import <Cocoa/Cocoa.h>
#include "NSutilities.h"
#include "cxcore.h"
#include "highgui.h"
#include "cv.h"

NSImage* opencvImageToNSImage(IplImage *img){
	char *d = img->imageData; // Get a pointer to the IplImage image data.
	
	NSString *COLORSPACE;
	if(img->nChannels == 1){
		COLORSPACE = NSDeviceWhiteColorSpace;
	}
	else{
		COLORSPACE = NSDeviceRGBColorSpace;
	}
	
	NSBitmapImageRep *bmp = [[NSBitmapImageRep alloc]  
initWithBitmapDataPlanes:NULL pixelsWide:img->width pixelsHigh:img- 
 >height bitsPerSample:img->depth samplesPerPixel:img->nChannels  
hasAlpha:NO isPlanar:NO colorSpaceName:COLORSPACE bytesPerRow:img- 
 >widthStep bitsPerPixel:0];
	
	// Move the IplImage data into the NSBitmapImageRep. widthStep is  
used in the inner for loop due to the
	//   difference between actual bytes in the former and pixel  
locations in the latter.
	// Assignment to colors[] is reversed because that's how an IplImage  
stores the data.
	int x, y;
	unsigned int colors[3];
	for(y=0; y<img->height; y++){
		for(x=0; x<img->width; x++){
			if(img->nChannels > 1){
				colors[2] = (unsigned int) d[(y * img->widthStep) + (x*3)]; //  
x*3 due to difference between pixel coords and actual byte layout.
				colors[1] = (unsigned int) d[(y * img->widthStep) + (x*3)+1];
				colors[0] = (unsigned int) d[(y * img->widthStep) + (x*3)+2];
			}
			else{
				colors[0] = (unsigned int)d[(y * img->width) + x];
				//NSLog(@"colors[0] = %d", colors[0]);
			}
			[bmp setPixel:colors atX:x y:y];
		}
	}
	
	NSData *tif = [bmp TIFFRepresentation];
	NSImage *im = [[NSImage alloc] initWithData:tif];
	
	return [im autorelease];
}

Objective C 来自UIImage的IplImage

// NOTE you SHOULD cvReleaseImage() for the return value when end of the code.
- (IplImage *)CreateIplImageFromUIImage:(UIImage *)image {
  // Getting CGImage from UIImage
  CGImageRef imageRef = image.CGImage;

  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  // Creating temporal IplImage for drawing
  IplImage *iplimage = cvCreateImage(
    cvSize(image.size.width,image.size.height), IPL_DEPTH_8U, 4
  );
  // Creating CGContext for temporal IplImage
  CGContextRef contextRef = CGBitmapContextCreate(
    iplimage->imageData, iplimage->width, iplimage->height,
    iplimage->depth, iplimage->widthStep,
    colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault
  );
  // Drawing CGImage to CGContext
  CGContextDrawImage(
    contextRef,
    CGRectMake(0, 0, image.size.width, image.size.height),
    imageRef
  );
  CGContextRelease(contextRef);
  CGColorSpaceRelease(colorSpace);

  // Creating result IplImage
  IplImage *ret = cvCreateImage(cvGetSize(iplimage), IPL_DEPTH_8U, 3);
  cvCvtColor(iplimage, ret, CV_RGBA2BGR);
  cvReleaseImage(&iplimage);

  return ret;
}

Objective C 添加到OS X状态托盘

@interface Tray : NSObject <NSApplicationDelegate> {
	NSStatusItem *trayItem;
}
@end

@implementation Tray

- (IBAction)testAction:(id)sender;
{
	NSLog(@"Hello World");
}

- (IBAction)quitAction:(id)sender;
{
	[NSApp terminate:sender];
}

- (void)applicationDidFinishLaunching:(NSNotification *)note;
{
	NSZone *zone = [NSMenu menuZone];
	NSMenu *menu = [[[NSMenu allocWithZone:zone] init] autorelease];
	NSMenuItem *item;
	
	item = [menu addItemWithTitle:@"Testing" action:@selector(testAction:) keyEquivalent:@""];
	[item setTarget:self];
	
	item = [menu addItemWithTitle:@"Quit" action:@selector(quitAction:) keyEquivalent:@""];
	[item setTarget:self];
	
	trayItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
	[trayItem setMenu:menu];
	[trayItem setHighlightMode:YES];
	[trayItem setTitle:@"HERE"];
}

- (void)dealloc;
{
	[trayItem release];
	[super dealloc];
}

@end

Objective C 在GCD中使用dispatch_once

dispatch_queue_t get_my_background_queue()
{
	static dispatch_once_t once;
	static dispatch_queue_t my_queue;
	dispatch_once(&once, ^{
		my_queue = dispatch_queue_create("com.example.background", NULL);
	});
	return my_queue;
}