【问题标题】:Problem in cropping the UIImage using CGContext?使用 CGContext 裁剪 UIImage 的问题?
【发布时间】:2011-02-08 22:56:45
【问题描述】:

我开发了一个简单的 UIApplication,我想在 CGContext 的帮助下裁剪 UIImage(.jpg 格式)。目前开发的代码如下,

CGImageRef graphicsOriginalImage = [originalImage.image CGImage];

UIGraphicsBeginImageContext(originalImage.image.size);

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGBitmapContextCreateImage(graphicOriginalImage);

CGFloat fltW = originalImage.image.size.width;
CGFloat fltH = originalImage.image.size.height;
CGFloat X = round(fltW/4); 
CGFloat Y =round(fltH/4);
CGFloat width = round(X + (fltW/2));
CGFloat height = round(Y + (fltH/2));   

CGContextTranslateCTM(ctx, 0, image.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGRect rect = CGRectMake(X,Y ,width ,height); 
CGContextDrawImage(ctx, rect, graphicOriginalImage);

croppedImage = UIGraphicsGetImageFromCurrentImageContext();

return croppedImage;

} 上面的代码工作正常,但无法裁剪图像。 我将得到相同的原始图像内存和裁剪图像内存(等于原始图像内存)。 上面的代码是正确的裁剪图像??????????????????

【问题讨论】:

    标签: iphone image crop


    【解决方案1】:

    这是将图像裁剪为 CGRect 的好方法:

    - (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect { //create a context to do our clipping in UIGraphicsBeginImageContext(rect.size); CGContextRef currentContext = UIGraphicsGetCurrentContext(); //create a rect with the size we want to crop the image to //the X and Y here are zero so we start at the beginning of our //newly created context CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height); CGContextClipToRect( currentContext, clippedRect); //create a rect equivalent to the full size of the image //offset the rect by the X and Y we want to start the crop //from in order to cut off anything before them CGRect drawRect = CGRectMake(rect.origin.x * -1, rect.origin.y * -1, imageToCrop.size.width, imageToCrop.size.height); //draw the image to our clipped context using our offset rect CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage); //pull the image from our cropped context UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext(); //pop the context to get back to the default UIGraphicsEndImageContext(); //Note: this is autoreleased return cropped; }

    或者其他方式:

    
    - (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
 {
      CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);

      UIImage *cropped = [UIImage imageWithCGImage:imageRef];
      CGImageRelease(imageRef);
    
    
  return cropped;

    }
    

    来自http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/

    【讨论】:

      【解决方案2】:

      您为绘制图像而创建的上下文与原始图像具有相同的大小。这就是它们具有相同大小的原因。

      如果您不想重新发明轮子,请查看 Google Code 上的 TouchCode 项目。你会发现 UIImage 类别可以完成这项工作(请参阅UIImage_ThumbnailExtensions.m)。

      【讨论】:

      • 暂时没有可用的下载。但您可以使用 Mercurial 克隆存储库。
      • 你知道使用 CGContext 方法使用 anf 的像素指针裁剪图像吗?
      猜你喜欢
      • 1970-01-01
      • 2012-05-20
      • 2017-01-11
      • 2010-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      相关资源
      最近更新 更多