How to Zoom + Crop a Image and Display the Croped Image on Imageview

Crop and Zoom Image without Moving Overlay View Instead Move Image in ImageView

Finally I got the solution by combining Cropper library and PhotoView libraries.

https://github.com/rameshkec85/AndroidCropMoveScaleImage

How to TOP CROP Image using PhotoView Android?

You can achieve a TopCrop scale changing the CENTER_CROP scale under updateBaseMatrix() in PhotoViewAttacher of Chris Banes`s PhotoView.

Here is the code:

else if (mScaleType == ScaleType.CENTER_CROP) {
float scale = Math.max(widthScale, heightScale);
mBaseMatrix.postScale(scale, scale);
//Changed dy = 0 for top crop
mBaseMatrix.postTranslate((viewWidth - drawableWidth * scale) / 2F, 0);

Source: http://www.widecodes.com/fxSzNWPeVX/android-image-view-top-crop.html


Since I so that people are requesting more scaleTypes in ImageView: https://code.google.com/p/android/issues/detail?id=58468 I added custom scale types to Chris Banes`s PhotoView.

I send a PR to him. Since I don't know if he will accept the PR(or when), you can get the project in my github: https://github.com/jonathanrz/PhotoView/ and add the aar to your project manually.

You can see an example of usage in CropTypeActivity in the sample project inside the library.

Objective-C iOS Crop Image Using Zoom?

I found an answer here that helped me do what I wanted
iOS: Cropping a still image grabbed from a UIImagePickerController camera with overlay

Code Snippet for those interested

float zoomScale = 1.0 / [scrollView zoomScale];

CGRect rect;
NSLog(@"contentOffset is :%f,%f",[scrollView contentOffset].x,[scrollView contentOffset].y);
rect.origin.x = fabsf([scrollView contentOffset].x * zoomScale);
rect.origin.y = fabsf([scrollView contentOffset].y * zoomScale);
rect.size.width = fabsf([scrollView bounds].size.width * zoomScale);
rect.size.height = fabsf([scrollView bounds].size.height * zoomScale);

UIGraphicsBeginImageContextWithOptions( CGSizeMake( rect.size.width,
rect.size.height),
NO,
0.);
[[imageView image] drawAtPoint:CGPointMake( -rect.origin.x, -rect.origin.y)
blendMode:kCGBlendModeCopy
alpha:1.];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

How do I capture a zoomed UIImageView inside of a Scrollview to crop?

UIImage* imageFromView(UIImage* srcImage, CGRect* rect)
{
CGImageRef cr = CGImageCreateWithImageInRect(srcImage.CGImage, *rect);
UIImage* cropped = [UIImage imageWithCGImage:cr];

CGImageRelease(cr);
return cropped;
}
-(void) doneEditing
{
//Calculate the required area from the scrollview
CGRect visibleRect;
float scale = 1.0f/scrollView.zoomScale;
visibleRect.origin.x = scrollView.contentOffset.x * scale;
visibleRect.origin.y = scrollView.contentOffset.y * scale;
visibleRect.size.width = scrollView.bounds.size.width * scale;
visibleRect.size.height = scrollView.bounds.size.height * scale;

FinalOutputView* outputView = [[FinalOutputView alloc] initWithNibName:@"FinalOutputView" bundle:[NSBundle mainBundle]];
outputView.image = imageFromView(imageView.image, &visibleRect);

[self.navigationController pushViewController:outputView animated:YES];
[outputView release];
}

Loading Orginal Image:

Sample Image

Zooming Image:

Sample Image

Finally Capturing the Image

Sample Image



Related Topics



Leave a reply



Submit