さて、注意しなければならないことがいくつかあります。
- UIView の代わりに UIImageView を渡していますか?
- 使用している場合、画像ビューでタッチ処理を有効にしましたか?
- UIImageView をサブクラス化し、それ
drawrect()
を処理するために使用します。
画像の一部 (猫など) のみが必要な場合は、UIBezierPath に従って画像をサブマスクする必要があります。
更新しました
以下は完全な動作例です。必要に応じて変更してください。
ViewController.h:
@interface ViewController : UIViewController
{
UIBezierPath *aPath;
}
@property (nonatomic,retain) NSMutableArray *pathArray;
@property (nonatomic,retain) NSMutableDictionary *dic;
@property (nonatomic,retain) IBOutlet UIImageView *imgView;
@end
ViewController.m:
@interface ViewController ()
- (IBAction)Crop:(id)sender;
@end
@implementation ViewController
@synthesize pathArray,dic,imgView;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void) setClippingPath:(UIBezierPath *)clippingPath : (UIImageView *)imgView;
{
NSLog(@"Mask Paths %@",clippingPath);
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.imgView.frame;
maskLayer.path = [clippingPath CGPath];
maskLayer.fillColor = [[UIColor whiteColor] CGColor];
maskLayer.backgroundColor = [[UIColor clearColor] CGColor];
self.imgView.layer.mask = maskLayer;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
self->aPath = [[UIBezierPath alloc]init];
[aPath moveToPoint:[mytouch locationInView:imgView]];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[aPath addLineToPoint:[mytouch locationInView:imgView
]];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (IBAction)Crop:(id)sender
{
[self setClippingPath:aPath :imgView];
}