ios - 裁剪后的 iOS 图像返回太大

标签 ios crop retina

一整天都在尝试解决这个问题,但没有成功。

差不多,我正在截取 View 的屏幕截图,然后尝试裁剪掉前 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;
}

最佳答案

我发现裁剪非常困惑!

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

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

也考虑这个有值(value)的类别......

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

希望能有所帮助

关于ios - 裁剪后的 iOS 图像返回太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23716648/

相关文章:

ios - 我的应用程序从 iOS8 上的 "Background App Refresh"设置中消失

ios - 适用于 iOS 的 Ionic 和 Cordova 中的奇怪内存泄漏

c# - 如何裁剪部分gif图像?

matlab - 如何在 MATLAB 中将裁剪后的人脸图像 reshape 为一维图像向量?

c++ - 如何裁剪不同矩阵中的一部分图像,但矩阵不会改变其在 openCV 中的大小。

ios - Realm swift 线程安全变量

jquery - 根据设备视网膜比率调整图像路径

css - 为视网膜显示调整图像大小

macos - Matlab 中 Retina 显示屏的屏幕尺寸错误

ios - 我的应用程序的旧版本在从 App Store 下载时运行正常,在使用 Xcode 运行时失败