在我的iPad应用程序中,我想制作UIView占据屏幕重要部分的屏幕截图.不幸的是,子视图非常嵌套,因此需要很长时间才能制作屏幕截图并使页面随后变形.
有没有比"通常"更快的方式?
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我想避免缓存或重构我的观点.
我正在尝试使用圆角在iPhone上绘制图像,以及联系人应用程序中的联系人图像.我有一些通常可以工作的代码,但它偶尔会在UIImage绘图例程中崩溃EXEC_BAD_ACCESS
- KERN_INVALID_ADDRESS
.我认为这可能与几个星期前我提出的裁剪问题有关,但我相信我正确地设置了裁剪路径.
这是我正在使用的代码 - 当它没有崩溃时,结果看起来很好,任何想要获得类似外观的人都可以免费借用代码.
- (UIImage *)borderedImageWithRect: (CGRect)dstRect radius:(CGFloat)radius {
UIImage *maskedImage = nil;
radius = MIN(radius, .5 * MIN(CGRectGetWidth(dstRect), CGRectGetHeight(dstRect)));
CGRect interiorRect = CGRectInset(dstRect, radius, radius);
UIGraphicsBeginImageContext(dstRect.size);
CGContextRef maskedContextRef = UIGraphicsGetCurrentContext();
CGContextSaveGState(maskedContextRef);
CGMutablePathRef borderPath = CGPathCreateMutable();
CGPathAddArc(borderPath, NULL, CGRectGetMinX(interiorRect), CGRectGetMinY(interiorRect), radius, PNDegreeToRadian(180), PNDegreeToRadian(270), NO);
CGPathAddArc(borderPath, NULL, CGRectGetMaxX(interiorRect), CGRectGetMinY(interiorRect), radius, PNDegreeToRadian(270.0), PNDegreeToRadian(360.0), NO);
CGPathAddArc(borderPath, NULL, CGRectGetMaxX(interiorRect), CGRectGetMaxY(interiorRect), radius, PNDegreeToRadian(0.0), PNDegreeToRadian(90.0), NO);
CGPathAddArc(borderPath, NULL, CGRectGetMinX(interiorRect), CGRectGetMaxY(interiorRect), radius, PNDegreeToRadian(90.0), PNDegreeToRadian(180.0), NO);
CGContextBeginPath(maskedContextRef);
CGContextAddPath(maskedContextRef, borderPath);
CGContextClosePath(maskedContextRef);
CGContextClip(maskedContextRef); …
Run Code Online (Sandbox Code Playgroud) 现在已经在Swift 3.0中删除了CGRectMake,CGPointMake,CGSizeMake等,有没有办法自动更新所有初始化,如CGRectMake(0,0,w,h) to CGRect(x:0,y:0,width:w,height:h)
.手动过程是非常痛苦的.
当我将代码转换为Current Swift语法时,不确定为什么Apple不会自动转换它...
在Swift编程中,如何裁剪图像并将其放在中心之后?
这就是我到目前为止所做的...我已成功裁剪图像,但我想把它放在中心之后
ImgView.image = OrigImage
var masklayer = CAShapeLayer()
masklayer.frame = ImgView.frame
masklayer.path = path.CGPath
masklayer.fillColor = UIColor.whiteColor().CGColor
masklayer.backgroundColor = UIColor.clearColor().CGColor
ImgView.layer.mask = masklayer
UIGraphicsBeginImageContext(ImgView.bounds.size);
ImgView.layer.renderInContext(UIGraphicsGetCurrentContext())
var image = UIGraphicsGetImageFromCurrentImageContext()
ImgView.image = image
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
更新:
let rect: CGRect = CGRectMake(path.bounds.minX, path.bounds.minY, path.bounds.width, path.bounds.height)
// Create bitmap image from context using the rect
let imageRef: CGImageRef = CGImageCreateWithImageInRect(image.CGImage, rect)
ImgView.bounds = rect
ImgView.image = UIImage(CGImage: imageRef)
Run Code Online (Sandbox Code Playgroud)
我能够通过获取path.bound和size来中心它并更改我的ImageView的边界.:)
我想用时控制图像排列UIViewContentModeScaleAspectFit
在UIImageView
.
例如,我有两个UIImageView
一个视图如上所述.这两个UIImageView
的contentMode是UIViewContentModeScaleAspectFit
.现在我想将图像3设置为右对齐,将图像4设置为左对齐,以便这两个图像可以在一起.
我尝试将contentMode设置为UIViewContentModeScaleAspect|UIViewContentModeLeft
,但它会让事情变得更糟.
任何建议表示赞赏.
我最近使用自动参考计数(ARC)开始了一个新项目.
当我分配CALayer的内容时:
UIView* view = ...
UIImage* image = ...
view.layer.contents = image.CGImage
Run Code Online (Sandbox Code Playgroud)
我收到了一个错误
使用ARC不允许将非Objective-C指针类型'CGImageRef'隐式转换为'id'
简单地铸造CGImageRef
到id
隐藏的错误,但我想知道如果ARC仍然正常工作呢?
core-animation core-graphics objective-c ios automatic-ref-counting
当从矩形原始图像中裁剪图像的正方形部分时,我正面临图像方向问题.当图像处于风景中时,它很好.但是当它是纵向时,似乎没有保留图像方向,这导致图像具有错误的方向和糟糕的裁剪:
func cropImage(cropRectangleCoordinates: CGRect) {
let croppedImage = originalImage
let finalCroppedImage : CGImageRef = CGImageCreateWithImageInRect(croppedImage.CGImage, cropRectangleCoordinates)
finalImage = UIImage(CGImage: finalCroppedImage)!
}
Run Code Online (Sandbox Code Playgroud)
我认为问题在于croppedImage.CGImage
.这里图像被转换为CGImage
,但似乎不保留方向.通过UIImage
仅使用来保持方向很容易,但是为了使裁剪,图像需要暂时进行CGImage
,这就是问题所在.即使我在转换回图像时重新定向图像UIImage
,它也可能处于正确的方向,但在裁剪时已经完成了损坏CGImage
.
这是一个很快的问题,所以请快速回答,因为Objective-C中的解决方案可能有所不同.
我试图允许用户在静态图像上的过滤器之间滑动.这个想法是当滤镜在其上方滚动时图像保持原位.Snapchat最近发布了一个实现此功能的版本.这段视频显示了我想要在1:05完成的事情.
到目前为止,我已经尝试将三个UIImageView放入原始图像的左侧和右侧的滚动视图中,并使用scrollView的contentOffset.x调整它们的帧origin.x和size.width.我发现这种想法在另一篇文章在这里.将左侧和右侧的内容模式更改为UIViewContentModeLeft和UIViewContentModeRight没有帮助.
接下来,我尝试将所有三个UIImageView堆叠在一起.我制作了两个CALayer蒙版并将它们插入到堆栈左侧和右侧的scrollView中,因此当您滚动蒙版时,将揭示过滤后的图像.这不适合我.任何帮助将不胜感激.
我正在编写一个带有图像的应用程序,除了图像中心的矩形外,还可以裁剪掉所有内容.(SWIFT)我无法让裁剪功能起作用.这就是我现在拥有的:
func cropImageToBars(image: UIImage) -> UIImage {
let crop = CGRectMake(0, 200, image.size.width, 50)
let cgImage = CGImageCreateWithImageInRect(image.CGImage, crop)
let result: UIImage = UIImage(CGImage: cgImage!, scale: 0, orientation: image.imageOrientation)
UIImageWriteToSavedPhotosAlbum(result, self, nil, nil)
return result
}
Run Code Online (Sandbox Code Playgroud)
我看了很多不同的指南,但似乎没有一个对我有用.有时图像旋转90度,我不知道它为什么这样做.
我写了一个UICropperViewController
,它在横向模式下完美地适用于图像.纵向模式下的图像存在很大问题.下图显示了带有黄色裁剪框的简单图片:
裁剪结果是:
现在谈到肖像图像我们得到了这种情况:
结果如下:
那么这里发生了什么?原始图像自动向左旋转.
我研究了很多,基本上找到了两个建议:
建议1
在裁剪之前保存图像方向并恢复它.
func didTapCropButton(sender: AnyObject) {
let originalOrientation = self.imageView.image?.imageOrientation;
// raw value of originalOrientation is `3` so its rotated to the right
let croppedCGImage = self.imageView.image?.cgImage?.cropping(to: self.cropArea);
// create a cropped cgImage
let croppedImage = UIImage(cgImage: croppedCGImage!, scale: (self.imageView.image?.scale)!, orientation: (originalOrientation)!);
// create the UIImage with the result from cgImage cropping and original orientation
if (self.callback != nil) {
self.callback.croppingDone(image: croppedImage);
}
self.dismiss(animated: true, completion: nil);
}
Run Code Online (Sandbox Code Playgroud)
但结果现在是:
显然这个建议不起作用,因为它只是简单地旋转已经裁剪的图像.
建议2
定位修复.我找到了以下代码,并承诺会修复错误: …
ios ×9
swift ×5
uiimage ×4
objective-c ×3
cgimage ×2
uiimageview ×2
calayer ×1
cgrectmake ×1
cocoa-touch ×1
crop ×1
imagefilter ×1
iphone ×1
performance ×1
quartz-core ×1
snapchat ×1
swift3 ×1
uiview ×1