在Swift中裁剪图像后,我得到的结果是倾斜90度 - 为什么?

时间:2016-11-29 00:51:29

标签: ios swift uiimage swift3

我正在为Swift https://github.com/budidino/ShittyImageCrop使用一个很好的github插件来负责裁剪图像。

我需要4:3的宽高比,所以我将这个控制器称为:

class Inventory {   // <--- Redesigned --->
  Long       id;
  Map <Long, ContainerQuantity>    cqsByProduct; // ProductId->ContainerQty
}

<class name="Inventory"> <!-- hbm mapping -->
   <id name="id"/>
   <map name="cqsByProduct">
      <key column="Inventory_ID" not-null="true"/>
      <map-key column="Product_ID"
      <one-to-many class="ContainerQuantity"/>
   </map>
</class>

Inventory table (in Database)
---------------
  id
 90210
 90211

class ContainerQuantity {
  Long                  id;
  Map<Long,Integer>     quantitiesByContainer;
}

<class name="ContainerQuantity"> <!-- hbm mapping -->
   <id name="id"/>
   <map name="quantitiesByContainer">
      <key column="CQ_ID" not-null="true"/>
      <map-key column="Container_ID"
      <one-to-many class="Quantity"/>
   </map>
</class>

ContainerQuantity table (for holding the cqsByProduct collection)
------------------------
   id     | Product_ID  | Inventory_ID (FK to collection owner)  
  101          1            90210
  102          1            90210
  103          2            90210

class Quantity {
  Integer               quantity;
  Container             container; 
  ContainerQuantity     cq;        
}

<class name="Quantity"> <!-- hbm mapping -->
    <composite-id>
       <key-many-to-one name="container" class="Container" column="Container_ID" />
       <key-many-to-one name="cq" class="ContainerQuantity" column="CQ_ID" />
    </composite-id>     
</class>

Quantity table  (for holding quantitiesByContainer collection)
--------------------
 Container_ID(FK) | Quantity | CQ_ID (FK to collection owner)
    10               25         101
    11               15         102
    10               33         103

现在,当我提供水平图像(宽度比较高)时 - 裁剪结果很好 - 我看到宽高比为4:3的照片作为输出。

但是当我提供垂直图像并尝试将其剪切时 - 我看到了倾斜的输出。例如,当普通照片是这样的时候:

enter image description here

垂直 - 并且倾斜 - 一个看起来像这样:

enter image description here

(对不起这里的低分辨率)。为什么会转移到一边?

我怀疑问题可能出现在裁剪按钮的逻辑中:

let shittyVC = ShittyImageCropVC(frame: (self.navigationController?.view.frame)!, image: image!, aspectWidth: 3, aspectHeight: 4)
self.navigationController?.present(shittyVC, animated: true, completion: nil)

1 个答案:

答案 0 :(得分:1)

ShittyImageCrop将裁剪后的图片直接保存到您的相册中,我无法使用垂直图像复制您的问题。

我看到您使用UIImageJPEGRepresentationUIImageWriteToSavedPhotosAlbum的{​​{1}}进行了对比,看来其他人在使用ShittyImageCrop后也出现了图像轮换问题。

查找iOS UIImagePickerController result image orientation after uploadiOS JPEG images rotated 90 degrees

修改

尝试从https://stackoverflow.com/a/27775741/611879

实施fixOrientation()

添加UIImageJPEGRepresentation

fixOrientation()

然后在使用func fixOrientation(img:UIImage) -> UIImage { if (img.imageOrientation == UIImageOrientation.Up) { return img } UIGraphicsBeginImageContextWithOptions(img.size, false, img.scale) let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height) img.drawInRect(rect) let normalizedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return normalizedImage } 之前执行此操作:

UIImageJPEGRepresentation

编辑2

请修改if let data = UIImageJPEGRepresentation(fixOrientation(croppedImage), 0.95) { try? data.write(to: URL(fileURLWithPath: path), options: [.atomic]) } init方法,将ShittyImageCrop替换为:

img = image