Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 根据UIImageView UIViewContentMode中的可见区域裁剪UIImage_Ios_Objective C_Uiview_Uiimageview_Uiimage - Fatal编程技术网

Ios 根据UIImageView UIViewContentMode中的可见区域裁剪UIImage

Ios 根据UIImageView UIViewContentMode中的可见区域裁剪UIImage,ios,objective-c,uiview,uiimageview,uiimage,Ios,Objective C,Uiview,Uiimageview,Uiimage,我正在UIImageView中设置UIImage。UIImageView可以将其内容模式设置为可用模式之一: typedef NS_ENUM(NSInteger, UIViewContentMode) { UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit, // contents scaled to fit with fixed aspect. remainder is transparent

我正在
UIImageView
中设置
UIImage
UIImageView
可以将其内容模式设置为可用模式之一:

typedef NS_ENUM(NSInteger, UIViewContentMode) {
    UIViewContentModeScaleToFill,
    UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
    UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
    UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
    UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
    UIViewContentModeTop,
    UIViewContentModeBottom,
    UIViewContentModeLeft,
    UIViewContentModeRight,
    UIViewContentModeTopLeft,
    UIViewContentModeTopRight,
    UIViewContentModeBottomLeft,
    UIViewContentModeBottomRight,
};
我想裁剪图像,使其比例与内容模式匹配。

这是如何实现的?

我不知道,您可以使用内容模式来实现这一点。请尝试使用以下核心图形代码:

- (UIImage *)cropVisiblePortionOfImageView:(UIImageView *)imageView {

  CGFloat zoomScaleX=imageView.frame.size.width/initialWidth;
  CGFloat zoomScaleY=imageView.frame.size.height/initialHeight;
  CGSize zoomedSize=CGSizeMake(initialWidth*zoomScaleX,initialHeight*zoomScaleY);

  UIGraphicsBeginImageContext(zoomedSize);
  [imageView.image drawInRect:CGRectMake(0, 0, zoomedSize.width, zoomedSize.height)];
  UIImage *zoomedImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  UIGraphicsBeginImageContext(CGSizeMake(initialWidth, initialHeight));
  [zoomedImage drawAtPoint:CGPointMake(imageView.frame.origin.x, imageView.frame.origin.y)];
  UIImage *cropedImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return cropedImage;
}

这将返回具有视图大小的图像。我需要全分辨率的图像。按比例裁剪