你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> 圖片展現、縮放、移動及裁減的實現

圖片展現、縮放、移動及裁減的實現

編輯:關於IOS

首先,讀取一個圖片文件到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];

 

注意,以上所有的代碼都是設想和查資料所得的參考,未經編碼測試,請讀者謹慎使用。

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved