我是ios的新手,我正在尝试一些可能非常简单的事情。 我正在制作一个绘制应用程序(它只是圆圈),我正在尝试将绘图保存到图书馆,但我无法理解如何做到这一点:(
到目前为止我的代码:
touchdrawview.h
#import <UIKit/UIKit.h>
@interface TouchDrawView : UIView
{
NSMutableDictionary *linesInProcess;
NSMutableArray *completeLines;
}
- (void)clearAll;
- (void)endTouches:(NSSet *)touches;
@end
touchdrawview.m
#import "TouchDrawView.h"
#import "Line.h"
@implementation TouchDrawView
- (id)initWithCoder:(NSCoder *)c
{
self = [super initWithCoder:c];
if (self) {
linesInProcess = [[NSMutableDictionary alloc] init];
// Don't let the autocomplete fool you on the next line,
// make sure you are instantiating an NSMutableArray
// and not an NSMutableDictionary!
completeLines = [[NSMutableArray alloc] init];
[self setMultipleTouchEnabled:YES];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 10.0);
CGContextSetLineCap(context, kCGLineCapRound);
// Draw complete lines in black
[[UIColor blackColor] set];
for (Line *line in completeLines) {
//CGContextMoveToPoint(context, [line begin].x, [line begin].y);
//CGContextAddLineToPoint(context, [line end].x, [line end].y);
CGRect rectangle = CGRectMake([line begin].x, [line begin].y, ([line end].x - [line begin].x), ([line end].y - [line begin].y));
CGContextAddEllipseInRect(context, rectangle);
CGContextStrokePath(context);
}
// Draw lines in process in red
[[UIColor redColor] set];
for (NSValue *v in linesInProcess) {
Line *line = [linesInProcess objectForKey:v];
//CGContextMoveToPoint(context, [line begin].x, [line begin].y);
//CGContextAddLineToPoint(context, [line end].x, [line end].y);
CGRect rectangle = CGRectMake([line begin].x, [line begin].y, ([line end].x - [line begin].x), ([line end].y - [line begin].y));
CGContextAddEllipseInRect(context, rectangle);
CGContextStrokePath(context);
}
}
- (void)clearAll
{
// Clear the collections
[linesInProcess removeAllObjects];
[completeLines removeAllObjects];
// Redraw
[self setNeedsDisplay];
}
- (void)touchesBegan:(NSSet *)touches
withEvent:(UIEvent *)event
{
for (UITouch *t in touches) {
// Is this a double tap?
if ([t tapCount] > 1) {
[self clearAll];
return;
}
// Use the touch object (packed in an NSValue) as the key
NSValue *key = [NSValue valueWithPointer:t];
// Create a line for the value
CGPoint loc = [t locationInView:self];
Line *newLine = [[Line alloc] init];
[newLine setBegin:loc];
[newLine setEnd:loc];
// Put pair in dictionary
[linesInProcess setObject:newLine forKey:key];
// There is a memory leak in this method
// You will find it using Instruments in the next chapter
}
}
- (void)touchesMoved:(NSSet *)touches
withEvent:(UIEvent *)event
{
// Update linesInProcess with moved touches
for (UITouch *t in touches) {
NSValue *key = [NSValue valueWithPointer:t];
// Find the line for this touch
Line *line = [linesInProcess objectForKey:key];
// Update the line
CGPoint loc = [t locationInView:self];
[line setEnd:loc];
}
// Redraw
[self setNeedsDisplay];
}
- (void)endTouches:(NSSet *)touches
{
// Remove ending touches from dictionary
for (UITouch *t in touches) {
NSValue *key = [NSValue valueWithPointer:t];
Line *line = [linesInProcess objectForKey:key];
// If this is a double tap, 'line' will be nil,
// so make sure not to add it to the array
if (line) {
[completeLines addObject:line];
[linesInProcess removeObjectForKey:key];
}
}
// Redraw
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches
withEvent:(UIEvent *)event
{
[self endTouches:touches];
}
- (void)touchesCancelled:(NSSet *)touches
withEvent:(UIEvent *)event
{
[self endTouches:touches];
}
- (void)dealloc
{
[linesInProcess release];
[completeLines release];
[super dealloc];
}
@end
touchdrawappdelegate.h
#import <UIKit/UIKit.h>
@class TouchDrawView;
@interface TouchTrackerAppDelegate : NSObject <UIApplicationDelegate> {
TouchDrawView *view;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
- (IBAction)guardar:(id)sender;
@end
touchdrawappdelegate.m
#import "TouchTrackerAppDelegate.h"
@implementation TouchTrackerAppDelegate
@synthesize window=_window;
-(UIImage *) ChangeViewToImage : (UIView *) view{
UIGraphicsBeginImageContext(view.bounds.size);
[TouchDrawView renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
- (IBAction)guardar:(id)sender{
NSLog(@"buh");
UIGraphicsBeginImageContext(CGSizeMake(320,480));
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(result, self, nil, nil);
}
此动作只看到白色图像,因为我无法将uiview转换为uiimage .. 怎么办呢?
此致
答案 0 :(得分:2)
CALayer有内容,您可以像这样保存
CGImageRef imageRef = CGImageCreateWithImageInRect((CGImageRef)penView.layer.contents, cropRect);
UIImage *penImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
添加框架QuartzCore
#import <QuartzCore/QuartzCore.h>
<强>更新强> 在你的代码中,应该是 TouchDrawView.layer
UIGraphicsBeginImageContext(view.bounds.size);
[TouchDrawView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();