【问题标题】:How to crop center part of large image in iOS?如何在 iOS 中裁剪大图像的中心部分?
【发布时间】:2015-01-15 07:01:44
【问题描述】:

当从中心裁剪图像时,裁剪图像将采用源图像的纵横比,但根据我的要求,纵横比会随着新的裁剪尺寸而变化。

我想获得具有新纵横比的图像的确切中心部分。例如,大图像的大小为 (320*480),然后我想裁剪大小为 (100,100) 的图像的中心部分,纵横比也将是100*100,无需外白或外黑,画质高。

裁剪功能:

- (UIImage *)cropImage:(UIImage*)image andFrame:(CGRect)rect {

    //Note : rec is nothing but the image frame which u want to crop exactly.

    rect = CGRectMake(rect.origin.x*image.scale,
                      rect.origin.y*image.scale,
                      rect.size.width*image.scale,
                      rect.size.height*image.scale);

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

请帮助我。

【问题讨论】:

  • 保留 Xcode 标签。这个问题根本与 Xcode IDE 无关。而且,你的代码在哪里?
  • 我已经用裁剪功能更新了我的问题
  • 任何人帮助我......我的项目尝试面对形状图像裁剪但不裁剪目标c

标签: ios objective-c image


【解决方案1】:

这可能会运行

- (UIImage *)imageByCroppingImage:(UIImage *)image toSize:(CGSize)size
{
    // not equivalent to image.size (which depends on the imageOrientation)!
    double refWidth = CGImageGetWidth(image.CGImage);
    double refHeight = CGImageGetHeight(image.CGImage);

    double x = (refWidth - size.width) / 2.0;
    double y = (refHeight - size.height) / 2.0;

    CGRect cropRect = CGRectMake(x, y, size.height, size.width);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);

    UIImage *cropped = [UIImage imageWithCGImage:imageRef scale:0.0 orientation:self.imageOrientation];
    CGImageRelease(imageRef);

    return cropped;
}

【讨论】:

  • 如何使中心部分像曲线形状一样纤细 任何人都知道这个概念,所以请大家告诉我
【解决方案2】:
var imageView = UIImageView()

// height and width values corresponds to rectangle height and width

imageView  = UIImageView(frame: CGRectMake(0, 0, width, height ))
imageView.image = UIImage(named: "Your Image Name")

// by   setting   content  mode  to  .ScaleAspectFill   image centrally  fill  in   imageView. image   might appear beyond  image frame.

imageView.contentMode = .ScaleAspectFill


// by   setting .clipsToBouds to true,  image set to image frame.

imageView.clipsToBounds = true
view.addSubview(imageView)

【讨论】:

  • 您好,欢迎来到 SO,当您回答时,请提供简短的解释,并格式化您的代码(四个空格即可)
【解决方案3】:

// 处理这段代码

     -(UIImage *)croppedImage
    {
    UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.bezierPath closePath];
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 0.0);
    _b_image = self.bg_imageview.image;
    CGSize imageSize = _b_image.size;
    CGRect imageRect = CGRectMake(0, 0, imageSize.width, imageSize.height);
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, [[UIScreen mainScreen] scale]);
    [self.bezierPath addClip];
    [_b_image drawInRect:imageRect];

    UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
return croppedImage;

}

【讨论】:

    【解决方案4】:

    根据图像计算裁剪矩形

    float imgHeight = 100.0f; //Any according to requirement
    float imgWidth = 100.0f; //Any according to requirement
    CGRect cropRect = CGRectMake((largeImage.size.width/2)-(imgWidth/2),largeImage.size.height/2)-(imgHeight/2),imgWidth,imgHeight);
    

    现在裁剪它

    CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
    // or use the UIImage wherever you like
    UIImage *croppedImg = [UIImage imageWithCGImage:imageRef]]; 
    CGImageRelease(imageRef); 
    

    【讨论】:

    • 感谢您的评论,我已经尝试过上面的代码,但它只是裁剪图像,但纵横比仍然是源图像。
    • 价格,我已经实现了你的代码,但是整个图像是用白色创建的。在我的情况下,大图像尺寸为 320*320。
    猜你喜欢
    • 2017-01-15
    • 2015-04-12
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 2019-05-13
    相关资源
    最近更新 更多