I have a UIImageView (initialized with Aspect Fill) inside a UIScrollView. The user shall use that to resize and position a UIImage to his like, press a button and then exactly what he sees on screen would be sent to a server, only in the original image coordinates. In other words, the resulting image would be bigger than the image on screen.
Here's the essential part of my code so far:
// select the original
CGFloat imageRatio = self.imageView.image.size.width / self.imageView.image.size.height;
CGFloat viewRatio = self.imageView.frame.size.width / self.imageView.frame.size.height;
CGFloat scale;
// get the scale factor of the image being "aspect-filled"
if(imageRatio < viewRatio) {
scale = self.imageView.image.size.width / self.imageView.frame.size.width;
} else {
scale = self.imageView.image.size.height / self.imageView.frame.size.height;
}
// get the cropping origin in the image's original coordinates
CGFloat originX = self.imageScrollView.contentOffset.x * scale;
CGFloat originY = self.imageScrollView.contentOffset.y * scale;
CGPoint origin = CGPointMake(originX, originY);
// get the cropping size in the image's original coordinates
CGFloat width = self.imageView.image.size.width / self.imageScrollView.zoomScale;
CGFloat height = self.imageView.image.size.height / self.imageScrollView.zoomScale;
CGSize size = CGSizeMake(width, height);
// create the new image from the original one
CGRect imageRect = CGRectMake(origin.x, origin.y, size.width, size.height);
struct CGImage *croppedCGImage = CGImageCreateWithImageInRect(self.imageView.image.CGImage, imageRect);
It already looks quite close to what I want. The origin calculation at least seems correct. However, there is still some difference in the resulting开发者_运维问答 image, which I guess comes from the different aspect ratios of the possible images. Probably I need to add scale
to my size
equations somehow, but I seriously just can't wrap my head around how.
Hopefully somebody can help me get this last two meters...
[EDIT]
According to the first comment, I tried out the following. But somehow my targetFrame
is always identical to scrollFrame
. Is it simply too late at night for me to get something right??
CGFloat zoom = self.imageScrollView.zoomScale;
// the full image resolution
CGRect fullImageFrame = CGRectMake(0, 0,
self.imageView.image.size.width,
self.imageView.image.size.height);
UIView *fullImageView = [[[UIView alloc] initWithFrame:fullImageFrame] autorelease];
// the theoretical size of the image on the screen
CGRect previewImageFrame = CGRectMake(0, 0,
self.imageView.frame.size.width * zoom,
self.imageView.frame.size.height * zoom);
UIView *previewImageView = [[[UIView alloc] initWithFrame:previewImageFrame] autorelease];
// the excerpt of previewImageFrame really visible
CGRect scrollFrame = CGRectMake(self.imageScrollView.contentOffset.x,
self.imageScrollView.contentOffset.y,
self.imageScrollView.frame.size.width,
self.imageScrollView.frame.size.height);
// that excerpt transferred to the full resolution image's coordinates
CGRect targetFrame = [fullImageView convertRect:scrollFrame fromView:previewImageView];
You may wish to explore:
(CGRect)convertRect:(CGRect)rect toView:(UIView *)view
This and other similar methods allow you to take a view and adjust its coordinate system based on placement in a superview other than the one it was initially intended or positioned for.
精彩评论