3

Been trying to fix this problem all day to no avail.

Pretty much, I'm taking a screenshot of the view, then trying to crop out the first 50px and a footer. Problem is that when I do this, the result is a little blowed up, and quality is lost. Here's what I wrote, which I think conforms to retina.

-(UIImage *)takeSnapShotAndReturn{
      //Take screenshot of whole view
    if([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
        UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,NO,[UIScreen  mainScreen].scale);
    }
    else{
        UIGraphicsBeginImageContext(self.view.window.bounds.size);
    }
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    combinedImage = [self cropOutArea:image withRectangle:CGRectMake(0, 50, 320, 467)];
    UIImageWriteToSavedPhotosAlbum(combinedImage, nil, nil, nil);
    UIGraphicsEndImageContext();

    return image;
}

-(UIImage *)cropOutArea:(UIImage*)image withRectangle:(CGRect)rectangle{
    if(image.scale > 1){
        rectangle = CGRectMake(rectangle.origin.x    * image.scale,
                               rectangle.origin.y    * image.scale,
                               rectangle.size.width  * image.scale,
                               rectangle.size.height * image.scale);
    }

    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rectangle);
    UIImage *result = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
    CGImageRelease(imageRef);

    return result;
}
JerryCrowley
  • 145
  • 1
  • 12

2 Answers2

2

I find cropping extremely confusing!

I'm not sure EXACTLY what you're trying to do, but this may be it .....

-(UIImage *)simplishTopCropAndTo640:(UIImage *)fromImage
        // moderately optimised!
    {
    float shortDimension = fminf(fromImage.size.width, fromImage.size.height);

    // 1.use CGImageCreateWithImageInRect to take only the top square...
    // 2. use drawInRect (or CGContextDrawImage, same) to scale...

    CGRect topSquareOfOriginalRect =
      CGRectMake(0,0, shortDimension,shortDimension);
                // NOT fromImage.size.width,fromImage.size.width);

    CGImageRef topSquareIR = CGImageCreateWithImageInRect(
                fromImage.CGImage, topSquareOfOriginalRect);

    CGSize size = CGSizeMake( 640,640 );
    CGRect sized = CGRectMake(0.0f, 0.0f, size.width, size.height);

    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
    CGContextRef cc = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(cc, kCGInterpolationLow);

    CGContextTranslateCTM(cc, 0, size.height);
    CGContextScaleCTM(cc, 1.0, -1.0);
    CGContextDrawImage(cc, sized, topSquareIR );
    // arguably, those three lines more simply...
    //[[UIImage imageWithCGImage:topSquareIR] drawInRect:sized];
    CGImageRelease(topSquareIR);

    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    result =
    [UIImage imageWithCGImage:result.CGImage
              scale:result.scale
              orientation: fromImage.imageOrientation];

    //consider...something like...
    //[UIImage imageWithCGImage:cgimg
    //       scale:3 orientation:fromImage.imageOrientation];

    return result;
    }

Consider also this valuable category .....

-(UIImage *)ordinaryCrop:(CGRect)toRect
    {
    // crops any image, to any rect.  you can't beat that

    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], toRect);
    UIImage *cropped = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return cropped;
    }

Finally don't forget this if you're using the camera "the most useful code in the universe!" iOS UIImagePickerController result image orientation after upload

Hope it helps somehow

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
0

Try setting this BOOL property before releasing result in cropOutArea.

result.layer.masksToBounds = YES
GlennRay
  • 959
  • 9
  • 18
  • It says that "layer" is not found on object type of UIImage * – JerryCrowley May 17 '14 at 23:19
  • Sorry, `UIImageView` has "layer", so you'd have to `UIImageView *resultView = [[UIImageView alloc] initWithImage:result]` first. then you could `resultView.layer.masksToBounds = YES`. And that means you'd be releasing a `UIImageView` instead of a `UIImage`, so you'd have to change the return of `cropOutArea`. – GlennRay May 18 '14 at 00:32
  • Ok. Tried it, but it still enlarges in the 4inch screen. The 3.5inch works though. – JerryCrowley May 18 '14 at 04:13