【问题标题】:Crop and recreate UIImage裁剪并重新创建 UIImage
【发布时间】:2014-05-25 17:14:48
【问题描述】:

我想在对其进行像素操作之前裁剪 UIImage(不是 imageview)。在 ios 框架中是否有可靠的方法来做到这一点?

以下是我正在使用的android中的相关方法:

http://developer.android.com/reference/android/graphics/Bitmap.html#createBitmap(android.graphics.Bitmap, int, int, int, int) http://developer.android.com/reference/android/graphics/Bitmap.html#createBitmap(int, int, android.graphics.Bitmap.Config)

【问题讨论】:

  • (不是 imageview).. 其中大多数是为 imageview 裁剪的,我没有使用那种方法。
  • 和?有很多关于 UIImage 的文章(注意:没有视图存在)。 CGImage.

标签: ios objective-c uiimage crop


【解决方案1】:

这对我有用:

-(UIImage *)cropCanvas:(UImage *)input x1:(int)x1 y1:(int)y1 x2:(int)x2 y2:(int)y2{
    CGRect rect = CGRectMake(x1, y1, input.size.width-x2-x1, input.size.height-y2-y1);
    CGImageRef imageref = CGImageCreateWithImageInRect([input CGImage], rect);
    UIImage *img = [UIImage imageWithCGImage:imageref];
    return img;
}

【讨论】:

    【解决方案2】:
    -(UIImage*)scaleToSize:(CGSize)size image:(UIImage*)image
    {
        UIGraphicsBeginImageContext(size);
    
        // Draw the scaled image in the current context
        [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
        // Create a new image from current context
        UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
        // Pop the current context from the stack
        UIGraphicsEndImageContext();
    
        // Return our new scaled image
        return scaledImage;
    }
    

    【讨论】:

      【解决方案3】:

      我建议你看看:

      • CoreAnimation Apple 框架用于创建图像上下文、在其中绘制并将其作为 UIImage 保存在 RAM 中或保存到磁盘。

      • CoreImage Apple 框架,如果您想要一个功能强大且完全可定制的图像处理解决方案。

      • 用于裁剪和调整图像大小的第三方库:CocoaPods 是快速集成此类库的好方法……这里列出了一些有趣的image manipulation pods

      【讨论】:

        猜你喜欢
        • 2010-09-14
        • 1970-01-01
        • 2018-07-08
        • 2011-02-07
        • 1970-01-01
        • 1970-01-01
        • 2023-04-11
        • 1970-01-01
        相关资源
        最近更新 更多