First, read an image file to a UIImage object and add it to a UIImageView. The size of the UIImageView is the same as that of the image. Ensure that all images are displayed.
// 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];
In this way, you can zoom in, zoom out, and move the UIImageView to view some images beyond the screen range.
Scale up or down an image to a proper size, move a part of the image to be reduced to the screen, and then cut it. In this way, the cut part is the text that requires image recognition to improve the recognition efficiency.
Use the pinch and pan gestures to zoom in and zoom in the image and pull the appropriate content into the screen.
Use the pan gesture to move the uiimageview.
-(Void) pan :( UIPanGestureRecognizer *) gesture
{
If (gesture. state = UIGestureRecognizerStateChanged) |
(Gesture. state = UIGestureRecognizerStateEnded )){
CGPoint location = [gesture locationInView: [self superview];
[Self setCenter: location];
}
}
Use the pinch gesture to scale the 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;
}
The scaled image is still targeted at the original image. Therefore, you need to take a picture of the screen and generate a picture for cropping. Because the reduction can only be performed in the visible area.
-(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;
}
Slide your fingers to draw a line into a rectangular frame, specify the area to be cut, and then confirm.
1. You can use UIBezierPath to draw a rectangle. The implementation method is like this,
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 addlinepoint: CGPointMake (200,140)];
[APath addLineToPoint: CGPointMake (0.0, 140)];
[APath closePath];
Refer to a subclass of uiview, which uses touchMove to call UIBezierPath for drawing.
@ 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. If you do not need UIBezierPath, you can use the Core Graphics framework API to draw a line.
@ Implementation GestureView
{
CGPoint _ originOfTouchPoint; // your fist touch detected in touchesBegan: method
CGPoint _ currentFingerPositionPoint; // the position you have dragged your finger
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;
// Calldrawrect: method to show updated line
[Self setNeedsDisplay];
}
}
-(Void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event
{
// Reset values
_ OriginOfTouchPoint = CGPointZero;
_ CurrentFingerPositionPoint = CGPointZero;
_ TouchStartedObject = nil;
}
@ End
How can I get the frame value of the rectangle?
Store the largest and smallest values of X and Y of the involved points in an array. After the touch ends, redraw the rectangle.
In this way, the rectangular area and frame value on the image are obtained. Based on the frame value, CGImageCreateWithImageInRect is used to obtain the image on the screen. Change the result as a UIImage object to UIImageView. Verify that the operation is correct.
// 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];
Note that all the code above is an assumption and reference for data query. Please use it with caution without code testing.