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

时间:2011-07-12 04:49:45

标签: iphone xcode image uiimageview

我正在为一个项目制作一个动画类型的app。我有一个空间来在imageView上绘制图像。然后,当您单击新页面按钮时,我需要该imageview中的图像移动到我在时间轴上的另一个图像视图。如果不让我知道您需要的其他信息,希望您能理解我的问题。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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

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

                 }];