如何将uiview转换为uiimage并保存到照片库

如何将uiview转换为uiimage并保存到照片库,uiview,uiimage,Uiview,Uiimage,我是ios的新手,我正在尝试一些可能非常简单的东西。 我正在制作一个绘图应用程序(它只做圆圈),我正在尝试将绘图保存到photolibrary,但我不知道如何做:( 到目前为止,我的代码是: touchdrawview.h #import <UIKit/UIKit.h> @interface TouchDrawView : UIView { NSMutableDictionary *linesInProcess; NSMutableArray *completeL

我是ios的新手,我正在尝试一些可能非常简单的东西。 我正在制作一个绘图应用程序(它只做圆圈),我正在尝试将绘图保存到photolibrary,但我不知道如何做:(

到目前为止,我的代码是:

touchdrawview.h

#import <UIKit/UIKit.h>


@interface TouchDrawView : UIView
{
    NSMutableDictionary *linesInProcess;
    NSMutableArray *completeLines;
}

- (void)clearAll;
- (void)endTouches:(NSSet *)touches;


@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
此操作仅看到白色图像,因为我无法将uiview转换为uiimage。。 如何做到这一点


如果CALayer有内容,可以这样保存

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

#import <QuartzCore/QuartzCore.h>

您好,谢谢您花时间来帮助我。我不太明白。我应该在哪里使用CGImageRef代码?它应该引用到我的视图?如果是,我该怎么做?在您编写的更新中,如果我编写touchdrawview.layer,我收到一条消息说在“touchdrawview”类型的对象上找不到属性“layer”。对于层,您需要将QuartzCore框架添加到项目中并添加导入。
CGImageRef imageRef = CGImageCreateWithImageInRect((CGImageRef)penView.layer.contents, cropRect);
 UIImage *penImage = [UIImage imageWithCGImage:imageRef];
 CGImageRelease(imageRef);
#import <QuartzCore/QuartzCore.h>
UIGraphicsBeginImageContext(view.bounds.size);
[TouchDrawView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();