Cocoa notes (23) Images
星期二, 五月 31st, 2011获取Image
1 | myImage = [UIImage imageNamed:@"icon.png"]; |
这个方法的缺陷是,image对象将会被IPhone系统cache住,并不受到该app的内存控制,不适合用于读取大图片。
一般使用此方法从app bundle中获取图片
1 2 3 |
获取app路径下的Documents路径的途径:
1 2 3 | NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); return [paths lastObject]; |
或
1 2 | return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; |
从URL获取图片
1 2 3 4 |
从Photo Album Library获得图片
通过UIImagePickerController,3个途径
UIImagePickerControllerSourceTypePhotoLibrary
UIImagePickerControllerSourceTypeSavedPhotosAlbum
UIImagePickerControllerSourceTypeCamera
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { SETIMAGE([info objectForKey:@"UIImagePickerControllerOriginalImage"]); [self dismissModalViewControllerAnimated:YES]; [picker release]; } - (void) pickImage: (id) sender { UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; ipc.delegate = self; ipc.allowsImageEditing = NO; [self presentModalViewController:ipc animated:YES]; } |
如果允许编辑,返回图片时
SETIMAGE([info objectForKey:@"UIImagePickerControllerEditedImage"]);
保存图片
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | - (NSString *) findUniqueSavePath { int i = 1; NSString *path; do { // iterate until a name does not match an existing file path = [NSString stringWithFormat:@"%@/Documents/IMAGE_%04d.PNG", NSHomeDirectory(), i++]; } while ([[NSFileManager defaultManager] fileExistsAtPath:path]); return path; } .... [UIImagePNGRepresentation(image) writeToFile:[self findUniqueSavePath] atomically:YES]; |
Page Scroll滚动图片
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | UIScrollView *sv = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, BASEHEIGHT)] autorelease]; sv.contentSize = CGSizeMake(NPAGES * 320.0f, sv.frame.size.height); sv.pagingEnabled = YES; sv.delegate = self; // Load in all the pages for (int i = 0; i < NPAGES; i++) { NSString *filename = [NSString stringWithFormat:@"image%d.png", i+1]; UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename]]; iv.frame = CGRectMake(i * 320.0f, 0.0f, 320.0f, BASEHEIGHT); [sv addSubview:iv]; [iv release]; } [self.view addSubview:sv]; |
绘图并绘制文字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | // Draw centered text into the context void centerText(CGContextRef context, NSString *fontname, float textsize, NSString *text, CGPoint point, UIColor *color) { CGContextSaveGState(context); CGContextSelectFont(context, [fontname UTF8String], textsize, kCGEncodingMacRoman); // Retrieve the text width without actually drawing anything CGContextSaveGState(context); CGContextSetTextDrawingMode(context, kCGTextInvisible); CGContextShowTextAtPoint(context, 0.0f, 0.0f, [text UTF8String], text.length); CGPoint endpoint = CGContextGetTextPosition(context); CGContextRestoreGState(context); // Query for size to recover height. Width is less reliable CGSize stringSize = [text sizeWithFont:[UIFont fontWithName:fontname size:textsize]]; // Draw the text [color setFill]; CGContextSetShouldAntialias(context, true); CGContextSetTextDrawingMode(context, kCGTextFill); CGContextSetTextMatrix (context, CGAffineTransformMake(1, 0, 0, -1, 0, 0)); CGContextShowTextAtPoint(context, point.x - endpoint.x / 2.0f, point.y + stringSize.height / 4.0f, [text UTF8String], text.length); CGContextRestoreGState(context); } - (UIImage *) createImageWithColor: (UIColor *) color { UIGraphicsBeginImageContext(CGSizeMake(SIDE_LENGTH, SIDE_LENGTH)); CGContextRef context = UIGraphicsGetCurrentContext(); // Create a filled ellipse [color setFill]; CGContextAddEllipseInRect(context, CGRectMake(0.0f, 0.0f, SIDE_LENGTH, SIDE_LENGTH)); CGContextFillPath(context); // Label with a number [[UIColor whiteColor] setFill]; NSString *numstring = [NSString stringWithFormat:@"%d", count++]; centerText(context, @"Georgia", 18.0f, numstring, CGPointMake(SIDE_LENGTH / 2.0f, SIDE_LENGTH / 2.0f), [UIColor whiteColor]); // Outline the circle CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]); CGContextAddEllipseInRect(context, CGRectMake(INSET_AMT, INSET_AMT, SIDE_LENGTH - 2.0f * INSET_AMT, SIDE_LENGTH - 2.0f * INSET_AMT)); CGContextStrokePath(context); UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return theImage; } |
Draw Image
1 2 3 4 5 6 7 8 9 10 11 12 13 | // calculate the fitted size CGSize size = [ImageHelper fitSize:image.size inSize:viewsize]; UIGraphicsBeginImageContext(viewsize); float dwidth = (viewsize.width - size.width) / 2.0f; float dheight = (viewsize.height - size.height) / 2.0f; CGRect rect = CGRectMake(dwidth, dheight, size.width, size.height); [image drawInRect:rect]; UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); |