青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

逛奔的蝸牛

我不聰明,但我會很努力

   ::  :: 新隨筆 ::  ::  :: 管理 ::

Learn Quartz

Intro to Quartz II

This tutorial is the second entry in the Introduction to Quartz series.Intro to Quartz Part I describes ideas that are essential to understand before reading this tutorial. Even if you've read it, you may want to review first. 

This tutorial explains how to create and use custom views, how to construct paths using NSBezierPath, and touches on a variety of topics related to using images with the NSImage class. 

This tutorial is written and illustrated by Scott Stevenson
 
Copyright © 2006 Scott Stevenson

Custom Views

A custom view is a subclass of NSView which serves a "blank canvas" for your drawing code. Once you have created the view class, you add drawing code using rects, paths and images in the drawRect method. 

To create a new view class in your project, choose File > New File in Xcode and select Objective-C NSView subclass
 
After the class file is created, you drag the header file to the NIB window in Interface Builder so it knows about the class.
 
Now the class has been added to the NIB file and can be used in a custom view.

Using a Custom View

Once the view has been added to the NIB file, open the Interface Builder palette and drag the CustomView icon out onto your application window.
 
While the custom view is still selected, bring up the inspector andchoose MyView from the Custom Class inspector dropdown.
 
The custom view now uses the MyView class. You should also selectSize from the inspector dropdown at this point to configure the sizing to your liking. 

The view will not display anything until drawing code is added todrawRect method in the MyView class.
 
In the above screenshot, we added a 210x210 rect and filled it using a blue NSColor. 

We implemented isFlipped to return YES so that the coordinates start in the upper-left instead of the bottom-left.

Paths

Rects can be used for basic graphics, but paths allow for much more complex shapes. The NSBezierPath class is used to create paths in Cocoa. 

Paths are mutable, so they can be changed or combined at any time. Path objects can draw themselves into a view. Here are some examples:
 
Paths can contain curved lines, arcs and rectanges. Each of these examples were drawn to the view by sending the stroke message to the path object. 

To create a curved line, you provide a start point, and end point, and two "control" points which act as gravity on the line shape. Control points are not needed for simple straight lines.
 

Filled Paths

Paths can be filled in addition to being stroked. The following examples first use the fill method with a white NSColor set, then stroke the path with a gray color set.
 
The paths are filled before being stroked so that the lines are visible.

Creating Paths

The following examples show how to create paths. All of the code assumes a flipped view with coordinates starting in the upper-left.
 
NSBezierPath * path = [NSBezierPath bezierPath]; [path setLineWidth: 4]; NSPoint startPoint = { 21, 21 }; NSPoint endPoint = { 128,128 }; [path moveToPoint: startPoint]; [path curveToPoint: endPoint controlPoint1: NSMakePoint ( 128, 21 ) controlPoint2: NSMakePoint ( 21,128 )]; [[NSColor whiteColor] set]; [path fill]; [[NSColor grayColor] set]; [path stroke];
 
Here's an example of appending a path to an existing path. The path being added is an arc:
 
NSBezierPath * path = [NSBezierPath bezierPath]; [path setLineWidth:4]; NSPoint center = { 128,128 }; [path moveToPoint: center]; [path appendBezierPathWithArcWithCenter: center radius: 64 startAngle: 0 endAngle: 321]; [[NSColor whiteColor] set]; [path fill]; [[NSColor grayColor] set]; [path stroke];
 
It's also easy to create a bezier path from a basic rect:
 
NSPoint origin = { 21,21 }; NSRect rect; rect.origin = origin; rect.size.width = 128; rect.size.height = 128; NSBezierPath * path; path = [NSBezierPath bezierPathWithRect:rect]; [path setLineWidth:4]; [[NSColor whiteColor] set]; [path fill]; [[NSColor grayColor] set]; [path stroke];
 

Complex Path Construction

The following examples incrementally build and draw several paths to construct the final image. All of the code goes into the custom view'sdrawRect method. 

First, we start by creating a basic oval in the center of the view.
 
// setup basic size and color properties float dm = 81 * 2; float rd = dm * 0.50; float qt = dm * 0.25; NSColor * white = [NSColor whiteColor]; NSColor * black = [NSColor blackColor]; NSBezierPath *path1, *path2, *path3, *path4; // find the center of the view float center = [self bounds].size.width * 0.50; float middle = [self bounds].size.height * 0.50; // create a rect in the center NSPoint origin = { center - rd, middle - rd }; NSRect mainOval = { origin.x, origin.y, dm, dm }; // create a oval bezier path using the rect path1 = [NSBezierPath bezierPathWithOvalInRect:mainOval]; [path1 setLineWidth:2.18]; // draw the path [white set];[path1 fill]; [black set];[path1 stroke];
 
Next, we define a path to overlay on the first one. The path is built in three parts, by appending three different arc paths to the original object. 

Note how we build the ellipse in the top half by negating the clockwiseoption.
 
// overlay a new path to draw the right side path2 = [NSBezierPath bezierPath]; // arc from the center to construct right side NSPoint mainOvalCenter = { center, middle }; [path2 appendBezierPathWithArcWithCenter: mainOvalCenter radius: rd startAngle: 90 endAngle: 270 clockwise: YES]; // add a half-size arc at the top: counter clockwise NSPoint curveOneCenter = { center, origin.y+qt }; [path2 appendBezierPathWithArcWithCenter: curveOneCenter radius: qt startAngle: 270 endAngle: 90 clockwise: NO]; // add a half-size arc in the bottom half NSPoint curveTwoCenter = { center, origin.y+rd+qt }; [path2 appendBezierPathWithArcWithCenter: curveTwoCenter radius: qt startAngle: 270 endAngle: 90 clockwise: YES]; // fill the path on the right side with black [black set];[path2 fill];
 
Finally, we build the paths for the two internal ellipses.
 
// calculate the size for each ellipses float dotDm = ( qt * 0.618 ); float dotRd = ( dotDm * 0.5 ); // create a rect at the center of the top section NSRect rect3 = NSMakeRect ( center - dotRd, origin.y + (qt - dotRd), dotDm, dotDm ); // create an oval with the rect and fill it with black path3 = [NSBezierPath bezierPathWithOvalInRect: rect3]; [black set];[path3 fill]; // copy the rect for the top ellipse and adjust the y axis NSRect rect4 = rect3; rect4.origin.y = NSMaxY(mainOval) - (qt + dotRd); // create an oval with the rect and fill it with white path4 = [NSBezierPath bezierPathWithOvalInRect: rect4]; [white set];[path4 fill];
 
The final result in our custom view.
 

Images

Cocoa's basic image class is NSImage. An image can draw itself into a view at various sizes and levels of opacity, or can be written to disk. 

Sample PhotoImage objects can be created from files on disk, loaded from data in memory, imported from the pasteboard, or drawn on the fly. 

Each NSImage instance manages a series of NSImageRep objects which are versions of the original image data suited to different contexts.
 
NSImage will automatically choose a representation when you draw into a view, but you can also use an image rep directly. NSBitmapImageRepis perhaps the most common.

Create and Display Images

Creating an NSImage from a file on disk and drawing it into a view is as simple as calling initWithContentsOfFile followed by drawInRect. To make things look nice, we'll center the image in the view and draw a border around it. 

First, we need to use NSImageInterpolationHigh to to get smooth bitmap scaling, then calculate the origin of the rect to draw the image in.
 
[[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh]; NSSize viewSize = [self bounds].size; NSSize imageSize = { 250, 156 }; NSPoint viewCenter; viewCenter.x = viewSize.width * 0.50; viewCenter.y = viewSize.height * 0.50; NSPoint imageOrigin = viewCenter; imageOrigin.x -= imageSize.width * 0.50; imageOrigin.y -= imageSize.height * 0.50; NSRect destRect; destRect.origin = imageOrigin; destRect.size = imageSize;
 
We load the image from disk and flip it since our view uses a flipped coordinate system. Once it's loaded, we draw it to the destination rect, than draw a border around the same rect.
 
NSString * file = @"/Library/Desktop Pictures/Plants/Leaf Curl.jpg"; NSImage * image = [[NSImage alloc] initWithContentsOfFile:file]; [image setFlipped:YES]; [image drawInRect: destRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0]; NSBezierPath * path = [NSBezierPath bezierPathWithRect:destRect]; [path setLineWidth:3]; [[NSColor whiteColor] set]; [path stroke]; [image release];
Here's the final result drawn into the custom view.
 
In this example, the image is loaded from disk right before being drawn to the view. This makes the code easier to follow, but it's very inefficient and makes window resizing very slow. 

A better approach is to load the image from disk once and store it in an instance variable for drawing later. One of the following examples demonstrates that.

Overlay Images with Transparency

Cocoa can easily draw images as partially transparent, as seen in the following example. First, we need to calculate the rects that we want to draw into, centering the images in the view.
 
[[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh]; NSSize viewSize = [self bounds].size; NSPoint viewCenter; viewCenter.x = viewSize.width * 0.50; viewCenter.y = viewSize.height * 0.50; NSSize large = { 250, 156 }; NSSize small = { 125, 78 }; NSPoint origin = viewCenter; origin.x -= (large.width+small.width) * 0.5; origin.y -= large.height * 0.5; // calculate the main image rect NSRect largeRect; largeRect.origin = origin; largeRect.size = large; // move to the right and resize NSRect smallRect1 = largeRect; smallRect1.origin.x += large.width; smallRect1.size = small; // move down NSRect smallRect2 = smallRect1; smallRect2.origin.y += small.height;
 
Next we need to load the images from disk and flip them to match our coordinate system.
 
NSString * file1 = @"/Library/Desktop Pictures/Plants/Purple Frond.jpg"; NSString * file2 = @"/Library/Desktop Pictures/Plants/Agave.jpg"; NSImage * image1 = [[NSImage alloc] initWithContentsOfFile:file1]; NSImage * image2 = [[NSImage alloc] initWithContentsOfFile:file2]; [image1 setFlipped:YES]; [image2 setFlipped:YES];
Now each image draws itself into the view twice: once in the main large rect, and once in a smaller rect. A single image object can draw itself any number of times. 

The images are drawn on top of each other in the large rect, with 80% and60% opacity, respectively. After that, each image draws itself into a separate smaller rect, fully opaque.
 
[image1 drawInRect: largeRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 0.8]; [image2 drawInRect: largeRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 0.6]; [image1 drawInRect: smallRect1 fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0]; [image2 drawInRect: smallRect2 fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
Finally, we draw borders around the images.
 
[[NSColor whiteColor] set]; NSBezierPath *path = [NSBezierPath bezierPathWithRect: largeRect]; [path appendBezierPathWithRect: smallRect1]; [path appendBezierPathWithRect: smallRect2]; [path setLineWidth:2.5]; [path stroke]; [image1 release]; [image2 release];
Here's the final result.
 
Resizing the window is still slow in this example, because we're still loading the image from disk frequently. The next example focuses on some optimization techniques.

Draw Directly to an Image

Hint: This technique is also useful if your application needs to generate images dynamically, or superimpose content on top of an existing image. The results can be written to a file.
We can optimize drawing multiple images to the screen by combining them in into one image, then draw the composite version to the view. 

This is called drawing into an "offscreen image" or "offscreen buffer", because the pixels are collected for later use instead of being displayed on the screen immediately.
 
To do this, we need to create an instance variable in the view namedcompositeImage, and add a method to generate the image. The generated image will be used in drawRect. 

Below is the first part of the code for createCompositeImage. First, we set up an array of image file names, and calculate the total size needed to hold all of them.
 
- (void)createCompositeImage { NSString * folder = @"/Library/Desktop Pictures/"; NSArray * files = [NSArray arrayWithObjects: @"Plants/Petals.jpg", @"Nature/Flowing Rock.jpg", @"Plants/Maple.jpg", nil]; // make a size big enough to hold all images int count = [files count]; NSRect rect = { 0,0, 150, 94}; NSSize compositeSize; compositeSize.width = (rect.size.width * count); compositeSize.height = rect.size.height;
Sample PhotoNow we'll create the compositeImage object and draw into it using the lockFocus method. Once lockFocus is called, all drawing goes directly into the image object until unlockFocus is called. 

Locking focus creates a new graphics context and also creates a new coordinate system where (0,0) is the upper-left corner of the image, rather than the view itself. For clarity, it's best to indent the code between locking and unlocking focus. 

The code below assumes standard accessors for compositeImage exist.
 
// -createCompositeImage continued... NSImage * compositeImage; compositeImage = [[NSImage alloc] initWithSize:compositeSize]; [compositeImage lockFocus]; // this image has its own graphics context, so // we need to specify high interpolation again [[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh]; int i; NSImage * image; NSString * file; for ( i = 0; i < count; i++ ) { // load the image from disk and draw it // into the composite image file = [folder stringByAppendingString: [files objectAtIndex:i]]; image = [[NSImage alloc] initWithContentsOfFile:file]; [image setFlipped:YES]; [image drawInRect: rect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0]; [image release]; image = nil; rect.origin.x += rect.size.width; // move right } [compositeImage unlockFocus]; [self setCompositeImage:compositeImage]; [compositeImage release]; } // end -createCompositeImage
 
Now we'll move on to drawing the composite image into the view.

Display the Composite Image

The composite image is drawn into the view in the NSView drawRectmethod, as shown below.
 
- (void) drawRect: (NSRect)aRect { NSImage * compositeImage = [self compositeImage]; if ( !compositeImage ) { // if the image is nil, we need to create it [self createCompositeImage]; compositeImage = [self compositeImage]; } // find the center of the view NSSize viewSize = [self bounds].size; NSPoint viewCenter; viewCenter.x = viewSize.width * 0.50; viewCenter.y = viewSize.height * 0.50; // calculate image rect to draw the image to NSSize size = [compositeImage size]; NSRect rect; rect.origin.x = viewCenter.x - (size.width * 0.50); rect.origin.y = viewCenter.y - (size.height * 0.50); rect.size = size; // draw composite image [compositeImage drawInRect: rect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0]; // draw border NSBezierPath * path = [NSBezierPath bezierPathWithRect:rect]; [path setLineWidth:2.5]; [[NSColor whiteColor] set]; [path stroke]; } // end of -drawRect
 
Here's our final result. You'll notice that resizing the window in this example is much faster than in the previous examples because we're only loading the image data once.
 
It's only necessary to draw into an offscreen image if you want to create a combination of multiple images. Individual images can just be set as instance variables and drawn as is.

Wrap Up

We've covered some intermediate Quartz topics here. If you'd like to see more tutorials like this, please make a donation below. The source code from this tutorial is included in the following zip file.
IntroToQuartzPartTwo Xcode 2.4 Project (56k)
Please consider the download your thank you gift for making a donation. It also contains a few small extras, including many more comments and an example of using Bindings and the Objective-C runtime to dynamically select a method to run.
Love it? Suggestions? Send feedback on this tutorial.
 
Show me a garden that's bursting into life.
Cocoa Dev Central is a servicemark of Tree House Ideas
Site design © 2004-2006 Scott Stevenson | Made with TextMate
From: http://www.cocoadevcentral.com/d/intro_to_quartz_two/
posted on 2011-12-02 00:16 逛奔的蝸牛 閱讀(723) 評論(0)  編輯 收藏 引用 所屬分類: Cocoa
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲一区3d动漫同人无遮挡| 亚洲高清视频一区二区| 久久精品国产99| 欧美中文在线观看国产| 欧美成人午夜激情在线| 欧美日韩一区二区免费视频| 国产精品海角社区在线观看| 国产在线拍偷自揄拍精品| 亚洲第一色中文字幕| 亚洲欧美电影院| 免费观看欧美在线视频的网站| 久久亚洲综合色| 一本色道久久综合亚洲二区三区| 久久精品国产综合| 国产精品一区二区三区成人| 亚洲激情av在线| 久久久www成人免费毛片麻豆| 亚洲欧洲av一区二区| 老司机免费视频一区二区三区| 在线综合亚洲欧美在线视频| 久久国产99| 洋洋av久久久久久久一区| 久久久久中文| 亚洲一区二区欧美日韩| 欧美另类专区| 亚洲欧洲日本在线| 午夜精品久久一牛影视| 久久综合狠狠| 在线视频国内自拍亚洲视频| 艳妇臀荡乳欲伦亚洲一区| 久久蜜桃香蕉精品一区二区三区| 国产精品久久久久婷婷| 亚洲精品免费在线播放| 欧美成人精品| 欧美成人69av| 久久久精品tv| 国产精品久久久久久av下载红粉| 免费成人你懂的| 国产精品你懂的在线| 亚洲国产精品热久久| 亚洲欧洲免费视频| 亚洲自拍偷拍福利| 欧美裸体一区二区三区| 欧美电影资源| 欧美精品在线一区二区三区| 久久久午夜精品| 国产欧美日韩三级| 麻豆精品视频在线观看| 国产酒店精品激情| 亚洲婷婷综合色高清在线 | 亚洲在线视频| 9久re热视频在线精品| 麻豆91精品| 一区精品在线| 亚洲电影免费观看高清完整版在线观看 | 欧美成人69av| 欧美激情一区二区三区不卡| 欧美激情影院| 欧美一区二区三区免费观看视频| 亚洲欧美视频在线| 一区二区三区在线视频观看| 午夜精品美女自拍福到在线 | 夜夜狂射影院欧美极品| 欧美v日韩v国产v| 亚洲午夜在线观看视频在线| 亚洲午夜视频在线| 亚洲欧美影院| 国产精品一区一区三区| 香港成人在线视频| 一区二区三区久久网| 欧美一级视频免费在线观看| 亚洲免费观看高清完整版在线观看| 一本色道久久综合亚洲二区三区| 亚洲桃花岛网站| 久久久久久有精品国产| 一区二区三区四区精品| 欧美日韩综合在线| 欧美大片免费观看| 99精品国产在热久久下载| 欧美一级精品大片| 欧美jizz19性欧美| 亚洲日本成人网| 欧美性事在线| 亚洲黑丝在线| 亚洲欧美激情一区二区| 国产欧美综合一区二区三区| 久久国产精品黑丝| 亚洲国语精品自产拍在线观看| 亚洲一区二区三区四区视频 | 欧美va亚洲va日韩∨a综合色| 亚洲欧洲日本mm| 亚洲第一视频网站| 欧美日韩亚洲综合一区| 午夜一区二区三区在线观看| 欧美成人亚洲成人| 性久久久久久久| 国产精品白丝黑袜喷水久久久| 亚洲一区国产视频| 美日韩精品免费| 亚洲图色在线| 亚洲成人自拍视频| 国产精品区二区三区日本| 亚洲美女免费视频| 亚洲人成小说网站色在线| 国产精品女人毛片| 免费在线成人| 欧美一区二区三区另类| 亚洲乱码国产乱码精品精98午夜| 1204国产成人精品视频| 国产精品白丝jk黑袜喷水| 蜜桃精品一区二区三区| 午夜影院日韩| 亚洲视频在线观看三级| 亚洲国产精品视频| 久久久精品动漫| 亚洲综合社区| 国产精品欧美一区二区三区奶水| 免费不卡中文字幕视频| 欧美有码视频| 亚洲影院免费观看| 日韩亚洲综合在线| 欧美激情精品久久久久久免费印度| 欧美亚洲综合网| 亚洲综合视频1区| 一本久道久久综合狠狠爱| 亚洲国产精品电影在线观看| 国产日韩欧美中文在线播放| 久久久久国产精品厨房| 亚洲女女女同性video| 999在线观看精品免费不卡网站| 欧美高清在线播放| 奶水喷射视频一区| 猛男gaygay欧美视频| 久久久久青草大香线综合精品| 午夜视频一区在线观看| 亚洲欧美日韩天堂一区二区| 99在线|亚洲一区二区| 日韩天天综合| 99精品黄色片免费大全| 日韩小视频在线观看| 亚洲美女精品一区| 99精品国产一区二区青青牛奶 | 国产精品成人观看视频免费| 欧美日韩美女在线| 欧美日韩在线综合| 国产精品久久久久aaaa樱花| 国产精品久久中文| 国产欧美精品在线| 韩日欧美一区二区| 欧美日韩午夜在线视频| 欧美日韩四区| 国产精品日日摸夜夜摸av| 国产精品免费aⅴ片在线观看| 国产精品美女午夜av| 国产一区二区三区久久悠悠色av| 女主播福利一区| 欧美精品一区视频| 国产精品女人久久久久久| 国产婷婷色一区二区三区在线| 韩日成人在线| 日韩一级黄色大片| 香蕉成人伊视频在线观看| 久久精品五月| 亚洲在线播放| 久久综合国产精品| 欧美国产日本在线| 99精品欧美一区二区三区综合在线 | 99视频超级精品| 午夜亚洲一区| 免费一级欧美片在线观看| 最新国产乱人伦偷精品免费网站| 一本一道久久综合狠狠老精东影业 | 欧美另类videos死尸| 国产欧美三级| 日韩视频精品在线| 久久国产黑丝| 午夜欧美大片免费观看 | 正在播放日韩| 久久精品国产欧美亚洲人人爽| 欧美黄色一区| 午夜日韩激情| 亚洲欧洲美洲综合色网| 欧美激情一区二区三区不卡| 欧美一级淫片播放口| 巨乳诱惑日韩免费av| 亚洲精品资源| 久久av红桃一区二区小说| 欧美日韩视频第一区| 一区二区在线观看av| 亚洲一区三区视频在线观看| 久久综合网色—综合色88| 亚洲视频在线播放| 欧美风情在线观看| 国产综合色精品一区二区三区| 一区二区91| 亚洲国产经典视频| 久久av一区二区| 国产精品久久久久久户外露出| 亚洲精品一二区| 欧美电影免费|