如何将 uiview 转换为 uiimage 并保存到照片库 [英] how to convert uiview to uiimage and save to photo gallery

查看:26
本文介绍了如何将 uiview 转换为 uiimage 并保存到照片库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ios 的新手,我正在尝试一些可能非常简单的东西.我正在制作一个绘图应用程序(它只是做圆圈),我正在尝试将绘图保存到照片库中,但我不明白如何做到这一点:(

i'm a newbie on ios and i'm trying something that it's probably very simple. i'm making a draw app (it just does circles) and i'm trying to save the draw to the photolibrary but i can't understand how to do that :(

到目前为止我的代码:

touchdrawview.h

touchdrawview.h

#import <UIKit/UIKit.h>


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

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


@end

touchdrawview.m

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

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

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..这怎么办?

This action only saw a white image because i can't convert the uiview to a uiimage.. how can this be done?

问候

推荐答案

CALayer有内容,可以这样保存

CALayer has contents, you can save it like this

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

添加框架 QuartzCore

add framework QuartzCore

#import <QuartzCore/QuartzCore.h>

更新在您的代码中,应该是 TouchDrawView.layer

UPDATE in your code, should be TouchDrawView.layer

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

这篇关于如何将 uiview 转换为 uiimage 并保存到照片库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆