使用CoreGraphices框架实现的图片擦除效果。如下:
//
// EraseImageView.h
// Eraser
//
// Created by scott.8an@gmail.com on 11-11-7.
// Copyright 2011 LittleWorn. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface EraseImageView : UIImageView {
@private
UIImageView *foregroundImageView;
BOOL canErase;
UIImage *backgroundImage;
UIImage *foregroundImage;
}
//init
-(id)initWithFrame:(CGRect)frame
backgroundImage:(UIImage*)bgImage
foregroundImage:(UIImage*)fgImage;
@end
//
// EraseImageView.m
// Eraser
//
// Created by scott.8an@gmail.com on 11-11-7.
// Copyright 2011 LittleWorn. All rights reserved.
//
#import "EraseImageView.h"
@implementation EraseImageView
#pragma mark life cycle
- (void)dealloc {
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame backgroundImage:(UIImage*)bgImage foregroundImage:(UIImage*)fgImage{
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
self.userInteractionEnabled = YES;
self.image = bgImage;
foregroundImageView = [[UIImageView alloc] initWithFrame:frame];
foregroundImageView.userInteractionEnabled = YES;
[foregroundImageView setImage:fgImage];
[self addSubview:foregroundImageView];
[foregroundImageView release];
}
return self;
}
#pragma mark override
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if ([touch view] == foregroundImageView){
canErase = YES;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if (canErase) {
CGPoint currentPoint = [touch locationInView:foregroundImageView];
UIGraphicsBeginImageContext(foregroundImageView.frame.size);
[foregroundImageView.image drawInRect:foregroundImageView.bounds];
CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(currentPoint.x, currentPoint.y, 30, 30));
foregroundImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
canErase = NO;
}
@end