Note that there are some explanatory texts on larger screens.

plurals
  1. POCropping a UIImage according to the viewport inside a UIScrollView
    primarykey
    data
    text
    <p>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.</p> <p>Here's the essential part of my code so far:</p> <pre><code>// 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 &lt; 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); </code></pre> <p>It already looks <strong>quite</strong> 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 <code>scale</code> to my <code>size</code> equations somehow, but I seriously just can't wrap my head around <strong>how</strong>.</p> <p>Hopefully somebody can help me get this last two meters...</p> <p><strong>[EDIT]</strong></p> <p>According to the first comment, I tried out the following. But somehow my <code>targetFrame</code> is always identical to <code>scrollFrame</code>. Is it simply too late at night for me to get something right??</p> <pre><code>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]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload