【问题标题】:Cropped iOS Image comes back too large裁剪的 iOS 图像返回太大
【发布时间】:2014-07-06 04:01:53
【问题描述】:

整天都在尝试解决这个问题,但无济于事。

差不多,我正在截取视图的屏幕截图,然后尝试裁剪前 50 像素和页脚。问题是,当我这样做时,结果有点夸张,质量下降。这是我写的,我认为符合视网膜。

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

【问题讨论】:

    标签: ios crop retina


    【解决方案1】:

    cropOutArea 中释放result 之前尝试设置此BOOL 属性。

    result.layer.masksToBounds = YES
    

    【讨论】:

    • 它说在 UIImage 的对象类型上找不到“层” *
    • 对不起,UIImageView 有“层”,所以你必须先UIImageView *resultView = [[UIImageView alloc] initWithImage:result]。那么你可以resultView.layer.masksToBounds = YES。这意味着您将发布UIImageView 而不是UIImage,因此您必须更改cropOutArea 的返回值。
    • 好的。试过了,4寸屏幕还是放大了。不过 3.5 英寸也可以。
    【解决方案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

    希望对你有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-16
      • 2011-10-28
      • 2018-06-18
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-22
      相关资源
      最近更新 更多