- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我对在 Swift 中使用 UIGraphicsGetImageFromCurrentImageContext()
感到困惑,这让我强制解包它,即使它是用 let
定义的。添加 ?
或 !
会使我的代码看起来很乱,并且让我在它之后更改所有内容。我不想在定义 scaledImage 时要求它。
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let scaledCIImage:CIImage = (scaledImage?.ciImage)! // makes me force upwrap
// gives error 'Value of optional type 'CIFilter?' not unwrapped; did you mean to use '!' or '?'?'
let filter = CIFilter(name: "CIAffineTile", withInputParameters:[kCIInputImageKey:scaledCIImage]).outputImage
最佳答案
有很多合法用途!
如果您知道何时正确使用它。
来自 UIGraphicsGetImageFromCurrentImageContext
的文档:
If the current context is nil or was not created by a call to UIGraphicsBeginImageContext(_:), this function returns nil.
由于您正在使用 UIGraphicsBeginImageContext
并且您当前的上下文不是 nil
,因此 UIGraphicsGetImageFromCurrentImageContext()
不会返回 nil
所以强制解包是安全的。
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()!
关于创建 CIImage
、CIFilter
和文件 CIImage
,使用 if let
安全解包.
if let scaledCIImage = scaledImage.ciImage,
let filter = CIFilter(name: "CIAffineTile", withInputParameters:[kCIInputImageKey:scaledCIImage]),
let outputImage = filter.outputImage {
// Do what you need with outputImage
}
根据您的评论更新:
鉴于 scaledmage.ciImage
是 nil
并且您想尝试从 cgImage
创建一个 CIImage
,您可以将 if let
的链更新为:
if let cgImage = scaledImage.cgImage {
let scaledCIImage = CIImage(cgImage: cgImage)
if let filter = CIFilter(name: "CIAffineTile", withInputParameters:[kCIInputImageKey:scaledCIImage]),
let outputImage = filter.outputImage {
// Do what you need with outputImage
}
}
关于swift - UIGraphicsGetImageFromCurrentImageContext() 给了我一个强制解包选项的链式 react ,我在 Swift 中不想要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50766671/
我正在拍摄屏幕照片并使用 UIGraphicsGetImageFromCurrentImageContext() 对其进行操作。一切都运转良好。然而,在 iPhone 4 上,分辨率看起来相当糟糕,因
UIGraphicsGetImageFromCurrentImageContext 返回自动释放的 UIImage。 想知道是否有办法从上下文中获取“分配”图像? 我想要等同于以下代码的东西,也许代码
我漏水了,但看不到哪里。 这是我的部分代码: + (UIImage *) imageWithColor:(UIColor *) color andSize:(CGSize) size { UI
我编写了以下代码片段来截取屏幕快照: UIGraphicsBeginImageContext(animationView.frame.size); [[window layer] renderInCo
UIImageView *cellimage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0 , 107, 70)]; 我确信以上声明将在视网
我正在使用 UIImagePickerControllerSourceTypeCamera 和自定义 cameraOverlayView 打开相机,这样我就可以在没有“使用照片”步骤的情况下拍摄多张照
我想将一个 UIImage 分成两个单独的图像。我使用以下(有效)代码。 UIImage *splitMeInTwo = [UIImage imageNamed:@"Star.png"]; UIIma
我正在尝试在 PDF 中创建页面的预览图像但是我在释放内存时遇到了一些问题。 我写了一个简单的测试算法来循环这个问题,应用在第 40 次迭代附近崩溃: NSArray *paths = NSSearc
我正在尝试使用长按手势识别器重新排序 UITableView 单元格。当我从单元格表示层使用 UIGraphicsGetImageFromCurrentImageContext() 时,CAAnima
我试图理解,UIgraphicsGetImageContext() 函数,根据我的理解,它是一个函数,它从当前位图上下文中获取图像,所以每当我在 Canvas 上绘制一些东西并调用此函数时,我都会绘制
我想在我的应用程序中进行基本的照片编辑,现在我需要能够在照片上添加文本。原始照片的宽度和高度大于 2000 像素,因此将在不修改其比例的情况下缩放以适合屏幕。 因此,我将图像放在 UIImageVie
我有一个 UIView,用户可以在其中绘制各种 UIBezierPaths。 我需要分析绘制的 BezierPaths 来处理某些模式。我还没有找到任何将 UIBezierPaths 转换为坐标/点列
我试图了解我的代码在使用队列时崩溃的方式,但在使用主线程时却能正常工作。此代码运行良好: - (void)penMoved:(NSSet *)touches { UITouch *touch
在我的绘图应用程序中,我使用 UIImage 来缓存绘图。 以下代码行导致内存问题: UIGraphicsBeginImageContextWithOptions(self.bounds.size,
我有这个 UIImage 的扩展: extension UIImage { func resizeImage(newWidth: CGFloat, newHeight: CGFloat? = nil
我目前正在使用 UIGraphicsGetImageFromCurrentImageContext() 将内容的图像保存到用户的相机胶卷中,但图像质量比我在模拟器中进行屏幕截图时差。有什么办法可以改善
快速代码 当我们获取一个UIView的截图时,我们通常会使用这段代码: UIGraphicsBeginImageContextWithOptions(frame.size, false, scale)
我的 iPhone 应用程序从服务器下载图像文件,将其存储到 NSTemporaryDirectory() 中,然后在 UI 中异步加载图像。代码流程是这样的: 显示带有加载事件指示器的 View 并
好的,我正在拍摄用 iPhone 上的相机拍摄的照片的屏幕截图。我将相机照片放入 UIImageView 中,并使用此类代码抓取它的屏幕截图(来自 http://www.skylarcantu.com
我想以特定大小截取特定位置的屏幕截图。我找到了这个。但它需要整个屏幕。我在哪里可以设置 CGRect。 UIGraphicsBeginImageContext(self.window.bounds.s
我是一名优秀的程序员,十分优秀!