欢迎访问 水平网    今天是:2017年11月22日 注册 | 登录 | 订阅 | 收藏
>> 苹果【Apple】 >> 编程应用 >> 图片展现、缩放、移动及裁减的实现
推荐文章
热点文章

图片展现、缩放、移动及裁减的实现

作者:未知,  来源:网络,  阅读:1541,  发布时间:2014-05-06  【放入收藏夹
首先,读取一个图片文件到UIImage对象中,添加到一个UIImageView视图中。UIImageView的size设置成和图片的size一样。保证图片能全部展现。
// Create the image from a png file
UIImage *image = [UIImage imageNamed:@"prgBinary.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

// Get size of current image
CGSize size = [image size];

// Frame location in view to show original image
[imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
[[self view] addSubview:imageView];
[imageView release];

这样UIImageView视图可以通过放大、缩小、移动的方式查看超出屏幕范围的部分图。
将图放大或者缩小到合适尺寸,移动需要裁减的图的一部分到屏幕中,再进行裁减。这样保证裁减到的部分是需要进行图像识别的文字,从而提升识别效率。

使用pinch手势和pan手势实现图像缩放和图像移动操作,将合适的内容完全拉入屏幕中。

使用pan手势移动uiimageview视图。

- (void)pan:(UIPanGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {

CGPoint location = [gesture locationInView:[self superview]];

[self setCenter:location];
}
}

使用pinch手势缩放uiimageview视图。
-(void)handlePinch:(UIPinchGestureRecognizer*)sender {
NSLog(@"latscale = %f",mLastScale);

mCurrentScale += [sender scale] - mLastScale;
mLastScale = [sender scale];

if (sender.state == UIGestureRecognizerStateEnded)
{
mLastScale = 1.0;
}

CGAffineTransform currentTransform = CGAffineTransformIdentity;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, mCurrentScale, mCurrentScale);
mGestureView.transform = newTransform;

}

缩放后的图片,裁减是还是针对了原来的图。因此需要将屏幕进行照相,生成一个图,供裁减操作。因为裁减只能在可看到的区域里进行。
- (UIImage *)imageWithUIView:(UIView *)view
{
CGSize screenShotSize = view.bounds.size;

UIImage *img;

UIGraphicsBeginImageContext(screenShotSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);

[view.layer renderInContext:UIGraphicsGetCurrentContext()];
img = UIGraphicsGetImageFromCurrentImageContext();

CGContextRestoreGState(context);
UIGraphicsEndImageContext();

return img;
}

 

 

 

采用手指滑动画线成矩形框的方式,指定裁减的区域,而后确认。

1、可以用UIBezierPath画一个矩形。它的实现方法大概这样,


UIBezierPath* aPath = [UIBezierPath bezierPath];
// Set the starting point of the shape.
[aPath moveToPoint:CGPointMake(100.0, 0.0)];
// Draw the lines
[aPath addLineToPoint:CGPointMake(200.0, 0.0)];
[aPath addLineToPoint:CGPointMake(200, 140)];
[aPath addLineToPoint:CGPointMake(0.0, 140)];
[aPath closePath];

参考一个uiview的子类,它使用touchMove调用UIBezierPath画图。

@implementation MyLineDrawingView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code

self.backgroundColor=[UIColor clear];
myPath=[[UIBezierPath alloc]init];
myPath.lineCapStyle=kCGLineCapRound;
myPath.miterLimit=0;
myPath.lineWidth=10;
brushPattern=[UIColor redColor];
}
return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
[brushPattern setStroke];
[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
// Drawing code
//[myPath stroke];
}

#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath moveToPoint:[mytouch locationInView:self]];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[myPath closePath];
[self setNeedsDisplay];
}

2、如果不用UIBezierPath的话,还可以直接使用 Core Graphics 框架的API进行画线。

@implementation GestureView

{
CGPoint _originOfTouchPoint; // your fist touch detected in touchesBegan: method
CGPoint _currentFingerPositionPoint; // the position you have dragged your finger to
CGFloat _strokeWidth; // the width of the line you wish to draw
id _touchStartedObject; // the object(UIView) that the first touch was detected on
}

// If you use Interface Builder to design your interface, Objects in a nib file are reconstituted and then initialized using
// their initWithCoder: method
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self) {
// Initialization code
_originOfTouchPoint = CGPointMake( 0.0, 0.0 );
_currentFingerPositionPoint = CGPointMake( 100.0, 100.0 );
_strokeWidth = 2.0;
}
return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor( context, [UIColor blueColor].CGColor );
CGContextSetLineWidth( context, _strokeWidth );
// fisrt point of line
CGContextMoveToPoint( context, _originOfTouchPoint.x, _originOfTouchPoint.y );
// last point of line
CGContextAddLineToPoint( context, _currentFingerPositionPoint.x, _currentFingerPositionPoint.y );
// draw the line
CGContextStrokePath( context );
}

#pragma mark touches

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// get starting point and first view touched (if you need to send that view messages)
_originOfTouchPoint = [[touches anyObject] locationInView:self];
_touchStartedObject = [[touches anyObject] view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint movedToPoint = [[touches anyObject] locationInView:self];
// if moved to a new point redraw the line
if ( CGPointEqualToPoint( movedToPoint, _currentFingerPositionPoint ) == NO )
{
_currentFingerPositionPoint = movedToPoint;
// calls drawRect: method to show updated line
[self setNeedsDisplay];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// reset values
_originOfTouchPoint = CGPointZero;
_currentFingerPositionPoint = CGPointZero;
_touchStartedObject = nil;
}

@end



怎么获取所画的矩形的frame值呢?

 

取所涉及的点的最大和最小的X和Y的值,保存到一个数组中。在touch结束之后,重新画矩形。
这样获得图像上的矩形区域和frame值,根据这个frame值,再使用CGImageCreateWithImageInRect取获得屏幕中截图。 将截图结果即一个UIImage对象换到UIImageView中。验证截图操作是否正确。

// Create rectangle that represents a cropped image
// from the middle of the existing image
CGRect rect = CGRectMake(size.width / 4, size.height / 4 ,
(size.width / 2), (size.height / 2));

// Create bitmap image from original image data,
// using rectangle to specify desired crop area
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

// Create and show the new image from bitmap data
imageView = [[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(0, 200, (size.width / 2), (size.height / 2))];
[[self view] addSubview:imageView];

[imageView release];

 

注意,以上所有的代码都是设想和查资料所得的参考,未经编码测试,请读者谨慎使用。

TGAS:图片缩放裁减
您可能还有兴趣查阅的相关文章
评论【共有0条评论】查看所有评论
昵称:(*)   邮箱:   QQ:   验证码: 看不清楚?点击刷新验证码