2011年8月16日 星期二

令UIImageView擁有簡單的Move, Zoom, Delete, Rotate功能

- (void)viewDidLoad
{
UIImageView* iconView = [[UIImageView alloc] initWithImage:icon];
[iconView setCenter:CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2)];
UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[iconView addGestureRecognizer:longPressGesture];
[longPressGesture release];
UIRotationGestureRecognizer* rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[iconView addGestureRecognizer:rotationGesture];
[rotationGesture release];
UIPinchGestureRecognizer* pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[iconView addGestureRecognizer:pinchGesture];
[pinchGesture release];
UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[iconView addGestureRecognizer:panGesture];
[panGesture release];
iconView.userInteractionEnabled = TRUE;
[self.view addSubview:iconView];
}

#pragma mark -
#pragma mark Gesture Callback
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
UIView *piece = gestureRecognizer.view;
CGPoint locationInView = [gestureRecognizer locationInView:piece];
CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];

piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
piece.center = locationInSuperview;
}
}

-(void)handleGesture:(UIGestureRecognizer*)gestureRecognizer
{
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

// Rotate
if([gestureRecognizer isMemberOfClass:[UIRotationGestureRecognizer class]])
{
UIRotationGestureRecognizer* rotationGestureRecognizer = (UIRotationGestureRecognizer*)gestureRecognizer;
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged)
{
[rotationGestureRecognizer view].transform = CGAffineTransformRotate([[rotationGestureRecognizer view] transform], [rotationGestureRecognizer rotation]);
[rotationGestureRecognizer setRotation:0];
}
}
// Zoom
else if([gestureRecognizer isMemberOfClass:[UIPinchGestureRecognizer class]])
{
UIPinchGestureRecognizer* pinchGestureRecognizer = (UIPinchGestureRecognizer*)gestureRecognizer;
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged)
{
[pinchGestureRecognizer view].transform = CGAffineTransformScale([[pinchGestureRecognizer view] transform], [pinchGestureRecognizer scale], [pinchGestureRecognizer scale]);
[pinchGestureRecognizer setScale:1];
}
}
// Menu - Delete
else if([gestureRecognizer isMemberOfClass:[UILongPressGestureRecognizer class]])
{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan)
{
UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem *resetMenuItem = [[UIMenuItem alloc] initWithTitle:@"Reset" action:@selector(deleteMenu:)];
CGPoint location = [gestureRecognizer locationInView:[gestureRecognizer view]];


[self becomeFirstResponder];
[menuController setMenuItems:[NSArray arrayWithObject:resetMenuItem]];
[menuController setTargetRect:CGRectMake(location.x, location.y, 0, 0) inView:[gestureRecognizer view]];
[menuController setMenuVisible:YES animated:YES];


[resetMenuItem release];
}
}
// Move
else if([gestureRecognizer isMemberOfClass:[UIPanGestureRecognizer class]])
{
UIView* piece = [gestureRecognizer view];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged)
{
UIPanGestureRecognizer* panGestureRecognizer = (UIPanGestureRecognizer*)gestureRecognizer;
CGPoint translation = [panGestureRecognizer translationInView:[piece superview]];


[piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
[panGestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
}
}

2011年8月15日 星期一

UIImagePickerController Camera獲取的圖片在CGContext下的正確繪製方法

UIImagePickerController Camera獲取的圖片,
本身是由它的Orientation決定它的方法,
在UIImage draw會無問題,
不過在CGImageRef下就要做一些手腳了

#define CC_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0f * (float)M_PI)
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(-90));
CGContextTranslateCTM(context, -size.height, 0);
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.height, size.width), image.CGImage);

由原圖獲取iPhone Photo Library Thumbnail Image的方法

這兩天要給公司完成一個有關相片的話Demo,
所以把以前研究圖片的代碼發上來,
可能有用,加上可以做個筆記,一舉兩得

+ (UIImage*)resizeImage:(UIImage*)image width:(int)width height:(int)height
{
// Create a thumbnail version of the image for the event object.
CGSize size = image.size;
CGSize croppedSize;
CGFloat offsetX = 0.0;
CGFloat offsetY = 0.0;

// check the size of the image, we want to make it
// a square with sides the size of the smallest dimension
if (size.width > size.height)
{
offsetX = (size.height - size.width) / 2;
croppedSize = CGSizeMake(size.height, size.height);
}
else
{
offsetY = (size.width - size.height) / 2;
croppedSize = CGSizeMake(size.width, size.width);
}

// Crop the image before resize
CGRect clippedRect = CGRectMake(offsetX * -1, offsetY * -1, croppedSize.width, croppedSize.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
// Done cropping

// Resize the image
UIGraphicsBeginImageContext(CGSizeMake(width, height));
CGContextRef bitmap = UIGraphicsGetCurrentContext();
// 以下代碼很特別,
// 如果圖片是由於Photo library/UIImagePickerController獲得來的,
// 圖片本身的方向會90度倒轉了,
// 只要設定一下CGContextSetTextMatrix為1.0, 0.0, 0.0, -1.0, 0.0, 0.0就會回復正常
CGAffineTransform transform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(bitmap, transform);
[[UIImage imageWithCGImage:imageRef] drawInRect:CGRectMake(0, 0, width, height)];
UIImage* thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Done Resizing

return thumbnail;
}