将 UIimage 视图设置为等于另一个 UIimage 视图

发布于 2024-11-19 08:07:54 字数 131 浏览 2 评论 0原文

我正在为一个项目制作一个小动画类型的应用程序。我有一个空间可以在 imageView 上绘制图像。然后,当您单击“新页面”按钮时,我需要将该图像视图中的图像移动到时间轴上的另一个图像视图中。希望您能理解我的问题,如果不让我知道您还需要哪些其他信息。

I am making a little animation type of app for a project. I have a space to draw images on an imageView. Then when you click the new page button I need the image in that imageview to move into another imageview I have on the timeline. Hopefully you understand my problem if not let me know what other information you need.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少跟Wǒ拽 2024-11-26 08:07:54

hai 检查此代码这将允许您绘制任何图像,然后您可以使用 nsdata 格式的 getimage 方法访问您的图像。然后你可以将其从 nsdata 转换为图像

.h File
//
//  SignatureCaptureImageView.h
//  TEST_DRAW_APP
//

 1. List item

//  Created by Talat Masud on 8/23/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface SignatureCaptureImageView : UIImageView {

    CGPoint lastPoint;

    BOOL mouseSwiped;   
    int mouseMoved;
}
-(NSData *)getImage;

@end


Implementation File


#import "SignatureCaptureImageView.h"


@implementation SignatureCaptureImageView


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
        self.userInteractionEnabled = YES;
        mouseMoved = 0;
    }
    return self;
}

- (id)initWithImage:(UIImage*)image {
    if ((self = [super initWithImage:image])) {
        // Initialization code
        self.userInteractionEnabled = YES;
        mouseMoved = 0;
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) 
    {
        //self.image = nil;
        return;
    }

    lastPoint = [touch locationInView:self];
    lastPoint.y -= 5;

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self];
    currentPoint.y -= 5;


    UIGraphicsBeginImageContext(self.frame.size);
    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

    mouseMoved++;

    if (mouseMoved == 10) {
        mouseMoved = 0;
    }

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        //self.image = nil;
        return;
    }


    if(!mouseSwiped) {
        UIGraphicsBeginImageContext(self.frame.size);
        [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        self.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
}

-(NSData *)getImage{
    return  UIImagePNGRepresentation(self.image);
}

- (void)dealloc {
    [super dealloc];
}


@end

hai Check This code This will allow you to draw any image and then you can access your image using method getimage in nsdata format. and then you can convert it into image from nsdata

.h File
//
//  SignatureCaptureImageView.h
//  TEST_DRAW_APP
//

 1. List item

//  Created by Talat Masud on 8/23/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface SignatureCaptureImageView : UIImageView {

    CGPoint lastPoint;

    BOOL mouseSwiped;   
    int mouseMoved;
}
-(NSData *)getImage;

@end


Implementation File


#import "SignatureCaptureImageView.h"


@implementation SignatureCaptureImageView


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
        self.userInteractionEnabled = YES;
        mouseMoved = 0;
    }
    return self;
}

- (id)initWithImage:(UIImage*)image {
    if ((self = [super initWithImage:image])) {
        // Initialization code
        self.userInteractionEnabled = YES;
        mouseMoved = 0;
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) 
    {
        //self.image = nil;
        return;
    }

    lastPoint = [touch locationInView:self];
    lastPoint.y -= 5;

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self];
    currentPoint.y -= 5;


    UIGraphicsBeginImageContext(self.frame.size);
    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

    mouseMoved++;

    if (mouseMoved == 10) {
        mouseMoved = 0;
    }

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        //self.image = nil;
        return;
    }


    if(!mouseSwiped) {
        UIGraphicsBeginImageContext(self.frame.size);
        [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        self.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
}

-(NSData *)getImage{
    return  UIImagePNGRepresentation(self.image);
}

- (void)dealloc {
    [super dealloc];
}


@end
云雾 2024-11-26 08:07:54

声明 2 个 UIImageViews ,第一个带有图像,第二个没有任何图像,即空 UIImageView,但具有原点和大小。

然后,当你想要制作动画时,请执行以下操作,

[UIView animateWithDuration:3.0 
                 animations:^{
                     CGRect sframe = mSecondImgView.frame;
                     mFirstImgView.frame = sframe;
                 }
                 completion:^(BOOL finished){

                 }];

declare 2 UIImageViews , first with the image and second without any image, ie., empty UIImageView, but having an origin and size.

Then, when u want to animate, do the following,

[UIView animateWithDuration:3.0 
                 animations:^{
                     CGRect sframe = mSecondImgView.frame;
                     mFirstImgView.frame = sframe;
                 }
                 completion:^(BOOL finished){

                 }];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文