裁剪的iOS图像回来太大了

时间:2014-05-17 22:22:48

标签: ios crop retina

一直试图解决这个问题,但无济于事。

差不多,我正在截取视图的截图,然后尝试裁剪出第一个50px和一个页脚。问题是,当我这样做时,结果有点爆炸,质量会丢失。这是我写的,我认为符合视网膜。

-(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;
}

2 个答案:

答案 0 :(得分:2)

我觉得裁剪非常混乱!

我不确定你要做什么,但这可能就是.....

-(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;
    }

还要考虑这个有价值的类别......

-(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;
    }

如果您使用相机“宇宙中最有用的代码!”,请不要忘记这一点。 iOS UIImagePickerController result image orientation after upload

希望它有所帮助

答案 1 :(得分:0)

尝试在result中发布cropOutArea之前设置此BOOL属性。

result.layer.masksToBounds = YES