Part 1 ios界面常用控件
1、要了解如何在您的代码中显示一个较大的非网络活动指示器,请参考UIActivityIndicatorView类参考。
2、要了解如何显示网络活动指示器,请参考UIApplication类参考中的networkActivityIndicatorVisible方法。
3、要了解表格视图可查看UITableViewStylePlain 无格式样式 / UITableViewStyleGrouped 分组格式样式
4、要了解更多有关在您的代码中使用日期时间选择器的内容,请参考UIDatePicker类参考
5、要了解更多有关在您的代码中使用详细信息展开按钮的内容,请参考UIButton类参考
6、要了解有关在您的代码中使用页指示符的更多内容,请参考UIPageControl类参考。
7、要了解更多有关在您的代码中使用选择器的内容,请参考UIPickerView
8、要了解更多有关在您的代码中使用进度视图的内容,请参考UIProgressView类参考
9、要了解更多有关在您的代码中使用搜索栏和范围栏的内容,请参考UISearchBar类参考。
10、要了解有关在您的代码中使用分段控件的更多内容,请参考UISegmentedControl类参考
11、要了解有关在您的代码中使用滑块的更多内容,请参考UISlider类参考
12、要了解与使用文本框,以及自定义显示图像和按钮的文本框的详情,请参考UITextField类参考
13、要了解可供您使用的键盘类型,请参考UIKeyboardType
14、触摸对象(UITouch)
Part 2 其他常用类的使用例子
NSArray
初始化
NSArray *arr = [NSArray alloc] initWithObjects:@"Me", @"Myself", @"I", nil];
NSMutableArray *mutable = [NSMutableArray alloc] init];
其他
[mutable addObject: @"One"];
[mutable sortUsingSelector: @selector( caseInsensitiveCompare: )];
遍历
void print( NSArray *array ) {
NSEnumerator *enumerator = [array objectEnumerator];
id obj;
while ( obj = [enumerator nextObject] ) {
printf( "%s\n", [obj description] cString] );
}
}
注:description method。它就像 Java 的 toString,會回傳物件的 NSString 表示法。
NSDictionary
初始化
NSDictionary *dictionary = [NSDictionary alloc] initWithObjectsAndKeys:
@"one", [NSNumber numberWithInt: 1],
@"two", [NSNumber numberWithInt: 2],
@"three", [NSNumber numberWithInt: 3],
nil];
NSMutableDictionary *mutable = [NSMutableDictionary alloc] init];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.9] forKey:NSImageCompressionFactor];
其他
[mutable setObject: @"Tom" forKey: @"tom@jones.com"];
遍历
void print( NSDictionary *map ) {
NSEnumerator *enumerator = [map keyEnumerator];
id key;
while ( key = [enumerator nextObject] ) {
printf( "%s => %s\n",
[key description] cString],
[[map objectForKey: key] description] cString] );
}
}
NSImage
初始化
NSImage *image = [NSImage alloc] initWithContentsOfFile:path];
[image setScalesWhenResized:YES];
[image setSize:NSMakeSize(1000.0, [image size].height * (1000.0/[image size].width))];
使用NSImage的lockFocus方法可以把NSGraphicsContext设置到它身上,原来是在当前窗体
NSImage *canvas = [NSImage alloc] initWithSize:canvasSize];
[canvas lockFocus];
//Draw things here.
[canvas unlockFocus];
在指定的矩形中显示图片
[originImage drawInRect:rect
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1.0];
保存成jpg图片
NSData *imageData = [image TIFFRepresentation];
//[foo TIFFRepresentation] writeToFile:@"/tmp/foo.tif" atomically:YES];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.9] forKey:NSImageCompressionFactor];
imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];
[imageData writeToFile:path atomically:YES];
NSGraphicsContext
所有的绘图操作其实都值对于当前的NSGraphicsContext起作用
//质量设置成高
[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
//打开反锯齿
[NSGraphicsContext currentContext] setShouldAntialias:YES];
NSRect
NSRect rect = NSMakeRect(border/2, border/2, canvasSize.width - border, canvasSize.height - border);
NSColor
[NSColor whiteColor] set];
NSBezierPath
NSBezierPath *whiteBorder = [NSBezierPath bezierPathWithRect:whiteBorderRect];
[whiteBorder setLineJoinStyle:NSRoundLineJoinStyle];
[whiteBorder setLineWidth:2];
[whiteBorder stroke];
CGImageSourceRef
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
NSDictionary *metaData = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source,0,NULL);//转出信息数据
NSDictionary *exifData = [metaData objectForKey:@"{Exif}"];
NSDictionary *tiffData = [metaData objectForKey:@"{TIFF}"];
//读取想要的信息
[metaData release];
CFRelease(source);
1、要了解如何在您的代码中显示一个较大的非网络活动指示器,请参考UIActivityIndicatorView类参考。
2、要了解如何显示网络活动指示器,请参考UIApplication类参考中的networkActivityIndicatorVisible方法。
3、要了解表格视图可查看UITableViewStylePlain 无格式样式 / UITableViewStyleGrouped 分组格式样式
4、要了解更多有关在您的代码中使用日期时间选择器的内容,请参考UIDatePicker类参考
5、要了解更多有关在您的代码中使用详细信息展开按钮的内容,请参考UIButton类参考
6、要了解有关在您的代码中使用页指示符的更多内容,请参考UIPageControl类参考。
7、要了解更多有关在您的代码中使用选择器的内容,请参考UIPickerView
8、要了解更多有关在您的代码中使用进度视图的内容,请参考UIProgressView类参考
9、要了解更多有关在您的代码中使用搜索栏和范围栏的内容,请参考UISearchBar类参考。
10、要了解有关在您的代码中使用分段控件的更多内容,请参考UISegmentedControl类参考
11、要了解有关在您的代码中使用滑块的更多内容,请参考UISlider类参考
12、要了解与使用文本框,以及自定义显示图像和按钮的文本框的详情,请参考UITextField类参考
13、要了解可供您使用的键盘类型,请参考UIKeyboardType
14、触摸对象(UITouch)
Part 2 其他常用类的使用例子
NSArray
初始化
NSArray *arr = [NSArray alloc] initWithObjects:@"Me", @"Myself", @"I", nil];
NSMutableArray *mutable = [NSMutableArray alloc] init];
其他
[mutable addObject: @"One"];
[mutable sortUsingSelector: @selector( caseInsensitiveCompare: )];
遍历
void print( NSArray *array ) {
NSEnumerator *enumerator = [array objectEnumerator];
id obj;
while ( obj = [enumerator nextObject] ) {
printf( "%s\n", [obj description] cString] );
}
}
注:description method。它就像 Java 的 toString,會回傳物件的 NSString 表示法。
NSDictionary
初始化
NSDictionary *dictionary = [NSDictionary alloc] initWithObjectsAndKeys:
@"one", [NSNumber numberWithInt: 1],
@"two", [NSNumber numberWithInt: 2],
@"three", [NSNumber numberWithInt: 3],
nil];
NSMutableDictionary *mutable = [NSMutableDictionary alloc] init];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.9] forKey:NSImageCompressionFactor];
其他
[mutable setObject: @"Tom" forKey: @"tom@jones.com"];
遍历
void print( NSDictionary *map ) {
NSEnumerator *enumerator = [map keyEnumerator];
id key;
while ( key = [enumerator nextObject] ) {
printf( "%s => %s\n",
[key description] cString],
[[map objectForKey: key] description] cString] );
}
}
NSImage
初始化
NSImage *image = [NSImage alloc] initWithContentsOfFile:path];
[image setScalesWhenResized:YES];
[image setSize:NSMakeSize(1000.0, [image size].height * (1000.0/[image size].width))];
使用NSImage的lockFocus方法可以把NSGraphicsContext设置到它身上,原来是在当前窗体
NSImage *canvas = [NSImage alloc] initWithSize:canvasSize];
[canvas lockFocus];
//Draw things here.
[canvas unlockFocus];
在指定的矩形中显示图片
[originImage drawInRect:rect
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1.0];
保存成jpg图片
NSData *imageData = [image TIFFRepresentation];
//[foo TIFFRepresentation] writeToFile:@"/tmp/foo.tif" atomically:YES];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.9] forKey:NSImageCompressionFactor];
imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];
[imageData writeToFile:path atomically:YES];
NSGraphicsContext
所有的绘图操作其实都值对于当前的NSGraphicsContext起作用
//质量设置成高
[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
//打开反锯齿
[NSGraphicsContext currentContext] setShouldAntialias:YES];
NSRect
NSRect rect = NSMakeRect(border/2, border/2, canvasSize.width - border, canvasSize.height - border);
NSColor
[NSColor whiteColor] set];
NSBezierPath
NSBezierPath *whiteBorder = [NSBezierPath bezierPathWithRect:whiteBorderRect];
[whiteBorder setLineJoinStyle:NSRoundLineJoinStyle];
[whiteBorder setLineWidth:2];
[whiteBorder stroke];
CGImageSourceRef
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
NSDictionary *metaData = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source,0,NULL);//转出信息数据
NSDictionary *exifData = [metaData objectForKey:@"{Exif}"];
NSDictionary *tiffData = [metaData objectForKey:@"{TIFF}"];
//读取想要的信息
[metaData release];
CFRelease(source);
=========================================================================
IOS 图形开发绘图小结
iPhone图形开发绘图教程是本文要介绍的内容,介绍了很多关于绘图类的使用,先来看详细内容讲解。
1、绘图总结:
绘图前设置:
- CGContextSetRGBFillColor/CGContextSetFillColorWithColor //填充色
- CGContextSetRGBStrokeColor/CGContextSetStrokeColorWithColor //笔颜色
- CGContextSetLineWidth //线宽度
绘图后设置:
注: 画完图后,必须先用CGContextStrokePath来描线,即形状,后用CGContextFillPath来填充形状内的颜色.
2.常见图形绘制:
- CGContextFillRect/CGContextFillRects
- CGContextFillEllipseInRect
- CGContextAddRect/CGContextAddRects
- CGContextAddEllipseInRect
- CGContextAddLines
- CGContextMoveToPoint
- CGContextAddLineToPoint
3.常见控制方法:
- CGContextSaveGState
- CGContextRestoreGState
4.创建内存图像context:
- CGBitmapContextCreate <-----CGContextRlease释放
- CGColorSpaceCreateWithName (KCGColorSpaceGenericRGB)
- CGColorSpaceRlease
- CGBitmapContextCreateImage() <-----CGImageRlease 释放.
- eg:
- CGContextRefMyCreateBitmapContext(intpixelsWide,intpixelsHigh)
- {
- CGContextRef context=NULL;
- CGColorSpaceRefcolorSpace;
- void* bitmapData;
- int bitmapByteCount;
- int bitmapBytesPerRow;
- bitmapBytesPerRow =(pixelsWide*4);
- bitmapByteCount =(bitmapBytesPerRow*pixelsHigh);
- colorSpace=CGColorSpaceCreateDeviceRGB();
- bitmapData=malloc(bitmapByteCount);
- if(bitmapData==NULL)
- {
- fprintf(stderr,"Memorynotallocated!");
- returnNULL;
- }
- context=CGBitmapContextCreate(bitmapData,
- pixelsWide, pixelsHigh, 8,
- bitmapBytesPerRow, colorSpace,
- kCGImageAlphaPremultipliedLast);
- if(context==NULL)
- {
- free(bitmapData);
- fprintf(stderr,"Contextnotcreated!");
- returnNULL;
- }
- CGColorSpaceRelease(colorSpace);
- returncontext;
- }
5.图形的变换:
- CGContextTranslateCTM
- CGContextRotateCTM
- CGContextScaleCTM
6.常用函数:
- CGRectContainsPoint();
- CGRectContainsRect();
- CGRectIntersectsRect();
- CGRectIntersection();
- CGPointEqualToPoint();
- CGSizeEqualToSize();
7.从原图片中取小图.
- CGImageCreateWithImageInRect
8.屏幕快照:
- #import "QuartzCore/QuartzCore.h"
- UIGraphicsBeginImageContext(yourView.frame.size);
- [[yourView layer] renderInContext:UIGraphicsGetCurrentContext()];
- UIImage*screenshot =UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- from:http://www.cppblog.com/zhangyuntaoshe/articles/123066.html
合并两张bit图到一张image的方法
- To graphically merge two images into a new image, you do something like this:
- UIImage *result = nil;
- unsignedchar *data = calloc(1,size.width*size.height*kBytesPerPixel);
- if (data != NULL) {
- // kCGImageAlphaPremultipliedLast 为预记录的#define value
- // 设置context上下文
- CGContextRef context = CGBitmapContextCreate(
- data, size.width, size.height, 8, size.width*kBytesPerPixel,
- CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast);
- if (context != NULL) {
- UIGraphicsPushContext(context);
- // Image 为下载的背景图片,用于比较context
- CGContextTranslateCTM(context, 0, size.height);
- CGContextScaleCTM(context, 1, -1);
- [image drawInRect:imageRect];
- [image2 drawInRect:image2Rect];
- UIGraphicsPopContext();
- CGImageRef imageRef = CGBitmapContextCreateImage(context);
- if (imageRef != NULL) {
- result = [UIImageimageWithCGImage:imageRef];
- CGImageRelease(imageRef);
- }
- CGContextRelease(context);
- }
- free(data);
- }
- return result;
关键方法:
- CGContextRef context = CGBitmapContextCreate();
- CGContextTranslateCTM();
- CGContextScaleCTM();
- CGImageRef imageRef = CGBitmapContextCreateImage(context);
- CGImageRelease(imageRef);
小结:iPhone图形开发绘图教程的内容介绍完了,希望本文对你有所帮助!