2012-02-07 93 views
0

我是ios的新手,我正在尝试这可能非常简单。 我正在做一个平局的应用程序(它只是做圆圈)和我试图挽救划向photolibrary,但我不明白怎么做:(如何将uiview转换为uiimage并保存为照片库

到目前为止我的代码:

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 .. 如何才能做到这一点?

问候

回答

2

的CALayer有内容,你可以将它保存这样

CGImageRef imageRef = CGImageCreateWithImageInRect((CGImageRef)penView.layer.contents, cropRect); 
UIImage *penImage = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef); 

附加框架QuartzCore

#import <QuartzCore/QuartzCore.h> 

UPDATE 在你的代码,应该是TouchDrawView.layer

UIGraphicsBeginImageContext(view.bounds.size); 
[TouchDrawView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+0

你好,谢谢你的时间试图帮助我。我不太了解它。我应该在哪里使用CGImageRef代码?它应该引用我的看法?如果是的话,我该怎么做? 在你写的更新,如果我写touchdrawview.layer我有一个消息说,'属性'层'没有找到类型的对象'touchdrawview' – Alapimba 2012-02-07 14:59:09

+0

层,你需要添加QuartzCore框架到您的项目并添加导入。 – SAKrisT 2012-02-07 15:22:43