作者:Codelif
学习了Cocoa的图片制作,受益很多。知道了图片是怎么画出来的。上层原理。可以对图片的图层 *** 作,如添加一个图层,删除图层,图片的混合,图片的缩放,矢量图图的制作,把字符串图片化,制作pdf的一些原理等。
// // NSImage+ZCPanel.h // Flected // // Created by zhuzhichao on 08-12-11. // copyright 2008 __MyCompanyname__. All rights reserved. #import <Cocoa/Cocoa.h> #import "CTGradIEnt.h" #define MAX_PIXEL_SIZE 600*800 #define PIC_WIDTH 800 #define PIC_HEIGHT 600 //Picture fram /* NSBMPfileType, NsgiFfileType, NSJPEGfileType,0); background-color:inherit"> NSPNGfileType,0); background-color:inherit"> NS@R_627_4301@fileType*/ @interface NSImage (ZCPanel) + (NSImage *) prettyGradIEntimage:(NSSize)gradIEntSize; // Generates a 256 by 256 pixel image with a complicated gradIEnt in it. + (NSImage *) reflectedImage:(NSImage *)sourceImage amountReflected:(float)fraction; + (NSImage *) createScalesImage:(NSImage *)sourceImage flipFlag:(BOol)bFlip amountReflected:( + (NSImage *) rotateImage:(NSImage*)sourceImage bydegrees:(float)deg; + (NSImage *) imageFromCGImageRef:(CGImageRef)image; //FROME CGImageRef to NSImage - (CGImageRef) nsImagetoCGImageRef;//FROME NSImage to CGImageRef - (BOol) setSmoothingEffect; //Set smoothing effect BOol) saveImage:(NSImage*)image saveType:(NSBitmAPImagefileType)storageType propertIEs:(NSDictionary *)propertIEs ToTarget:(Nsstring *) targePath; - (CGImageRef)@R_404_4148@Forfile: (Nsstring*)name atPath: (Nsstring*)filePath; @end // AppController+ZCBundImage.m #import "NSImage+ZCPanel.h" float distance(NSPoint aPoint); enum pixelComponents { red, green, blue, Alpha }; #define PI 3.14159265358979323846264338327950288 @implementation NSImage (ZCPanel) ////倒立,深度 + (NSImage *)reflectedImage:(NSImage *)sourceImage amountReflected:(float)fraction { NSImage *reflection = [[NSImage alloc] initWithSize:[sourceImage size]]; [reflection setFlipped:YES]; [reflection lockFocus]; CTGradIEnt *fade = [CTGradIEnt gradIEntWithBeginningcolor:[NScolor colorWithCalibrateDWhite:1.0 Alpha:0.5] endingcolor:[NScolor clearcolor]]; [fade fillRect:NSMakeRect(0, 0, [sourceImage size].wIDth, [sourceImage size].height*fraction) angle:90.0]; [sourceImage drawAtPoint:NSMakePoint(0,0) fromrect:NSZeroRect operation:NSCompositeSourceIn fraction:1.0]; [reflection unlockFocus]; return [reflection autorelease]; } //缩放到目的大小,太小了不缩放添加背景 + (NSImage *)createScalesImage:(NSImage *)sourceImage flipFlag:(float)fraction { //set flipped [sourceImage setScalesWhenResized:YES]; if(bFlip) { [sourceImage setFlipped:YES]; } //source picture size NSSize srcSize = [sourceImage size]; unsigned int uiWIDth= srcSize.wIDth; unsigned int uiHeight= srcSize.height; //target bg picture size int bgWIDth = PIC_WIDTH; int bgHeight = PIC_HEIGHT; NSSize tarSize =NSMakeSize(bgWIDth, bgHeight); if(uiWIDth>=bgWIDth && uiHeight >= bgHeight) { [sourceImage setSize:tarSize]; return [[sourceImage copy] autorelease]; if(uiWIDth>bgWIDth && uiHeight < bgHeight) [sourceImage setSize:tarSize]; //target bg picture NSImage *targetimage = [[NSImage alloc] initWithSize:tarSize]; [targetimage lockFocus]; //fill target bg picture,using white color [[NScolor whitecolor] set]; NSRectFill(NSMakeRect(0, bgWIDth, bgHeight*fraction)); //draw [sourceImage drawAtPoint:NSMakePoint(0,(bgHeight - uiHeight)*0.5) fromrect:NSZeroRect operation:NSCompositeSourceIn fraction:1.0]; [targetimage unlockFocus]; return [targetimage autorelease]; } if(uiWIDth<bgWIDth && uiHeight >bgHeight) //target bg picture NSImage *targetimage = [[NSImage alloc] initWithSize:tarSize]; [targetimage lockFocus]; [[NScolor whitecolor] set]; NSRectFill(NSMakeRect(0, bgWIDth*fraction)); //draw [sourceImage drawAtPoint:NSMakePoint((bgWIDth- uiWIDth)*0.5, 0) fromrect:NSZeroRect operation:NSCompositeSourceIn fraction:1.0]; [targetimage unlockFocus]; return [targetimage autorelease]; else //(uiWIDth<bgWIDth && uiHeight < bgHeight) //[sourceImage setSize:tarSize]; [sourceImage drawAtPoint:NSMakePoint((bgWIDth - uiWIDth)*0.5, (bgHeight - uiHeight)*0.5) fromrect:NSZeroRect operation:NSCompositeSourceIn fraction:1.0]; //按照图片的中心旋转90.180.270,360度 float)deg NSSize srcsize= [sourceImage size]; float srcw = srcsize.wIDth; float srch= srcsize.height; float newdeg = 0; //旋转弧度 //double ratain = ((deg/180) * PI); NSRect r1; if(0< deg && deg <=90) r1 = NSMakeRect(0.5*(srcw -srch), 0.5*(srch-srcw), srch, srcw); newdeg = 90.0; if(90< deg && deg <=180) r1 = NSMakeRect(0, srcw, srch); newdeg = 180.0; if(180< deg && deg <=270) newdeg = 270.0; if(270< deg && deg <=360) newdeg = 360; //draw new image NSImage *rotated = [[NSImage alloc] initWithSize:[sourceImage size]]; [rotated lockFocus]; NSAffinetransform *transform = [NSAffinetransform transform]; [transform translateXBy:(0.5*srcw) yBy: (0.5*srch)]; //按中心图片旋转 [transform rotateBydegrees:newdeg]; //旋转度数,rotateByradians:使用弧度 [transform translateXBy:(-0.5*srcw) yBy: (-0.5*srch)]; [transform concat]; [[sourceImage bestRepresentationForDevice: nil] drawInRect: r1];//矩形内画图 //[sourceImage drawInRect:r1 fromrect:NSZeroRect operation:NSCompositecopy fraction:1.0]; //[sourceImage drawAtPoint:arge/*NSZeroPoint*/ fromrect:NSMakeRect(arge.x, arge.y,ww ,wh)/*NSZeroRect*/ operation:NSCompositecopy fraction:1.0]; [rotated unlockFocus]; return [rotated autorelease]; } //save image to file - (BOol)saveImage:(NSImage*)image //source image saveType:(NSBitmAPImagefileType)storageType //save type "NSJPEGfileType" propertIEs:(NSDictionary *)propertIEs //propertIEs "NSImageCompressionFactor = (NSNumber)0.8" ToTarget:(Nsstring *) targePath //save path NSData *tempdata; NSBitmAPImageRep *srcImageRep; BOol reflag = NO; [image lockFocus]; srcImageRep = [NSBitmAPImageRep imageRepWithData:[image TIFFRepresentation]]; tempdata = [srcImageRep representationUsingType:storageType propertIEs:propertIEs]; reflag = [tempdata writetofile:targePath atomically:YES]; [image unlockFocus]; return reflag; // --------------------------------------------------------------------------------------------------------------------- - (CGImageRef)@R_404_4148@Forfile: (Nsstring*)name atPath: (Nsstring*)filePath // use ImageIO to get a @R_404_4148@ for a file at a given path CGImageSourceRef isr = NulL; Nsstring * path = [filePath stringByExpandingTildeInPath]; CGImageRef image = NulL; // path = [path stringByAppendingPathComponent: name]; // create the CGImageSourceRef isr = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath: path], NulL); if (isr) // create a @R_404_4148@: // - specify max pixel size // - create the @R_404_4148@ with honoring the EXIF orIEntation flag (correct transform) // - always create the @R_404_4148@ from the full image (ignore the @R_404_4148@ that may be embedded in the image - // reason: our MAX_ICON_SIZE is larger than existing @R_404_4148@) image = CGImageSourceCreate@R_404_4148@AtIndex (isr, (CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: MAX_PIXEL_SIZE], kCGImageSource@R_404_4148@MaxPixelSize,255); color:inherit"> (ID)kcfBooleanTrue, kCGImageSourceCreate@R_404_4148@Withtransform,92); margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; line-height:14px; List-style-type:decimal; padding-top:2px; padding-right:0px; padding-bottom:2px; padding-left:0px"> (ID)kcfBooleanTrue, kCGImageSourceCreate@R_404_4148@FromImageAlways,255); color:inherit"> NulL] ); CFRelease(isr); return image; } + (NSImage*) imageFromCGImageRef:(CGImageRef)image NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0); CGContextRef imageContext = nil; NSImage* newImage = nil; // Get the image dimensions. imageRect.size.height = CGImageGetHeight(image); imageRect.size.wIDth = CGImageGetWIDth(image); // Create a new image to receive the Quartz image data. newImage = [[[NSImage alloc] initWithSize:imageRect.size] autorelease]; [newImage lockFocus]; // Get the Quartz context and draw. imageContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]; CGContextDrawImage(imageContext, *(CGRect*)&imageRect, image); [newImage unlockFocus]; return newImage; - (CGImageRef)nsImagetoCGImageRef//:(NSImage*)image; NSData * imageData = [self TIFFRepresentation]; CGImageRef imageRef; if(imageData) CGImageSourceRef imageSource = CGImageSourceCreateWithData( (CFDataRef)imageData, NulL); imageRef = CGImageSourceCreateImageAtIndex( imageSource,255); color:inherit"> return imageRef; BOol)setSmoothingEffect NSBitmAPImageRep *rep = [[self representations] objectAtIndex: 0]; NSSize size = NSMakeSize ([rep pixelsWIDe], [rep pixelsHigh]); if(size.wIDth >0 && size.height>0) [self setSize: size]; return YES; return NO; // Generates a 256 by 256 pixel image with a complicated gradIEnt in it. + (NSImage *) prettyGradIEntimage:(NSSize)gradIEntSize NSImage *newImage = [[self alloc] initWithSize:gradIEntSize]; // In this case, the pixel dimensions match the image size. int pixelsWIDe = gradIEntSize.wIDth; int pixelsHigh = gradIEntSize.height; NSBitmAPImageRep *bitmapRep = [[NSBitmAPImageRep alloc] initWithBitmapDataPlanes: nil // Nil pointer makes the kit allocate the pixel buffer for us. pixelsWIDe: pixelsWIDe // The compiler will convert these to integers, but I just wanted to make it quite explicit pixelsHigh: pixelsHigh bitsPerSample: 8 samplesPerPixel: 4 // Four samples, that is: RGBA hasAlpha: YES isPlanar: NO // The math can be simpler with planar images, but performance suffers.. colorSpacename: NSCalibratedRGBcolorSpace // A calibrated color space gets us colorSync for free. bytesPerRow: 0 // Passing zero means "you figure it out." bitsPerPixel: 32]; // This must agree with bitsPerSample and samplesPerPixel. char *imageBytes = [bitmapRep bitmapData]; // -bitmapData returns a voID*, not an NSData object ;-) int row = pixelsHigh; while(row--) int col = pixelsWIDe; while(col--) { int pixelindex = 4 * (row * pixelsWIDe + col); imageBytes[pixelindex + red] = rint(fmod(distance(NSMakePoint(col/1.5,(255-row)/1.5)),255.0)); //red imageBytes[pixelindex + green] = rint(fmod(distance(NSMakePoint(col/1.5, row/1.5)),0); background-color:inherit">// green imageBytes[pixelindex + blue] = rint(fmod(distance(NSMakePoint((255-col)/1.5,0); background-color:inherit">// blue imageBytes[pixelindex + Alpha] = 255; // Not doing anything tricky with the Alpha value here... } [newImage addRepresentation:bitmapRep]; return [newImage autorelease]; float distance(NSPoint aPoint) // Stole this from some guy named Pythagoras.. Returns the distance of aPoint from the origin. return sqrt(aPoint.x * aPoint.x + aPoint.y *aPoint.y); //open selecter file panel /*static NSArray* openImagefiles() { // Get a List of extensions to filter in our NSOpenPanel. NSOpenPanel* panel = [NSOpenPanel openPanel]; [panel setCanChooseDirectorIEs:YES]; // The user can choose a folder; images in the folder are added recursively. [panel setCanChoosefiles:YES]; [panel setAllowsMultipleSelection:YES]; if ([panel runModalForTypes:[NSImage imageUnfilteredfileTypes]] == NSOKbutton) return [panel filenames]; return nil; }*/ @H_404_1391@ 总结以上是内存溢出为你收集整理的对NSImage的一个扩展全部内容,希望文章能够帮你解决对NSImage的一个扩展所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)