【问题标题】:how to crop image to image with offset and size? [duplicate]如何将图像裁剪为具有偏移量和大小的图像? [复制]
【发布时间】:2014-10-19 10:30:22
【问题描述】:

我正在制作一个需要将图像裁剪为另一个图像的应用程序。 我想将源图像(如绿色矩形)裁剪为目标图像(如白色矩形)。我可以获得源图像和目标图像的大小以及偏移量 x 和 y 。我怎样才能得到该裁剪图像并将其保存到库中? 您可以在此处查看图片:

如何裁剪到该图像?如果可以的话,请给我一个示例源代码 非常感谢

【问题讨论】:

    标签: ios objective-c iphone crop


    【解决方案1】:

    检查以下代码:

    -(UIImage *)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
    {
    
    UIGraphicsBeginImageContext(newSize); 
    
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];  
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();   
    
    UIGraphicsEndImageContext();   
    
    return newImage;
    }
    

    查看以下链接以供参考:

    1. http://code4app.net/ios/Image-crop-demo/501e1f3f6803faea5d000000

    2. http://code4app.net/ios/Photo-Cropper-View-Controller/4f95519c06f6e7d870000000

    3. http://code4app.net/ios/Image-Cropper/4f8cc87f06f6e7d86c000000

    4. http://code4app.net/ios/Simple-Image-Editor-View/4ff2af4c6803fa381b000000

    从这里获取示例代码。然后你可以自定义你自己的。

    【讨论】:

    【解决方案2】:

    使用此方法并传递图像,矩形作为参数。您可以在cropRect中指定y偏移和x偏移

    -(UIImage *)cropImage:(UIImage *)image rect:(CGRect)cropRect
    {
       CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
       UIImage *cropedImg = [UIImage imageWithCGImage:imageRef]; 
       CGImageRelease(imageRef);
       return cropedImg;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-05
      • 2014-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-12
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      相关资源
      最近更新 更多