惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
T
The Blog of Author Tim Ferriss
B
Blog
L
LangChain Blog
Y
Y Combinator Blog
美团技术团队
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
量子位
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
Check Point Blog
D
Docker
小众软件
小众软件
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 聂微东
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
IT之家
IT之家

Swift

都 vibe coding 了,为啥不把 Rust 项目 rewrite 到 Swift ? 应该怎么解读 swiftui 的崩溃报告? FoundationModels 框架实践 Swift DocC 构建的博客 Swift server 要支棱起来了 求问贴,如果在 swiftUI 中接入 MySQL C client Swift 萌新, DispatchQueue 的一个问题 分享一下一个 Web 前端刚学习 Swift 时候的心得博文 5 分钟用 Swift 写个 ripripgrep Swift 操作 plist 文件 使用 SwiftUI + TCA 重构 Instagram Clone:从 Flutter 到 SwiftUI 的迁移实践 激发编程激情,轻松掌握 Swift SwiftUI 分享个基于 UICollectionView 封装的数据驱动和声明式组件库 ListKit 问一个关于 CALayer 的资源释放问题 SwiftUI 的 TextEditor 在 macOS Sequoia 15.0 上输入中文时又会这样一跳一跳的了 ScreenCaptureKit 里面的 SCStreamOutput 没有被顺利触发,是那个步骤错了,求解惑 似乎 SwiftUI app 在 macOS 15 上启动速度变快很多? 请教一个关于 Swift 的基础小问题 离线 iOS app 也需要 ICP ? 请教 SwiftUI 布局的问题 ios 17 widget 无法正确更新 core data 有没有 Swift ui 版本的 tailwind css? swiftui 里,这里用 hstack,为什么是垂直布局,而不是水平布局? 大家觉得 Swift 的参数必须按序提供怎么样? Swift 开源的组件库好像不是很多啊 Swift 如何像 Python 那样 崩溃了输出 backtrace 或者至少是哪行出错了。。。 请教一下各位 iOSer 巨佬们一个 SwiftUI sheet 组件的问题 请教一下各位 iOSer 巨佬们一个 SwiftUI 中 HStack 的布局问题 swiftui 有好用的组件库吗? 求问:苹果手机是否无法像安卓一样,可以扫描出本地的音乐?
Swift 中系统 API delegate 参数一定要在当前类实现传 self 吗
jiangzm · 2024-09-29 · via Swift

因为要写原生插件,swift 刚接触,语法比 objc 好看多了。

在开发相机组件时遇到一个问题,AVCapturePhotoCaptureDelegate 契约用自定义 UIView/UIViewController 实现没问题, 换成独立的类实现就回调不了,不是什么原因。

上代码:

TakePhotoDelegate.swift

import Foundation
import AVFoundation
import UIKit

public typealias TakePhotoCallback = ((String, String) -> Void)?;

class TakePhotoWithCompletion : NSObject, AVCapturePhotoCaptureDelegate {

    // ...
    var completion: TakePhotoCallback;
    
    init(callback: TakePhotoCallback) {

        self.completion = callback;
        super.init();
    }

    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    
        // todo sth
    
        self.completion!("{file path}", "{error}");
        
    }
}


CameraxView.swift

import AVFoundation
import UIKit

class CameraxView: UIView {

    // ...

    @objc
    func takePhoto(options: Dictionary<String, String>, completion: TakePhotoCallback) {
        
        let photoSettings = AVCapturePhotoSettings.init(format:[AVVideoCodecKey:AVVideoCodecType.jpeg])
        let delegate = TakePhotoWithCompletion.init(callback: completion)
        
        imageOutPut?.capturePhoto(with:photoSettings, delegate: delegate)

    }

  // ...
}

点击拍照按钮能听到快门声,但是 photoOutput 没有回调

如果换成如下自定义 UIView 实现 AVCapturePhotoCaptureDelegate 没有问题,能正常回调

import AVFoundation
import UIKit

class CameraxView: UIView, AVCapturePhotoCaptureDelegate {

    // ...
    var completion: TakePhotoCallback = nil;

    @objc
    func takePhoto(options: Dictionary<String, String>) {
        
        let photoSettings = AVCapturePhotoSettings.init(format:[AVVideoCodecKey:AVVideoCodecType.jpeg])

        self.completion = completion
        
        imageOutPut?.capturePhoto(with:photoSettings, delegate: self)

    }

    @objc
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    
        // todo sth
    
        self.completion!("{file path}", "{error}");
        
    }

    // ...

}

有这个差异的原因是因为系统 API 的限制吗还是 swift 语言层面问题, 如果 takePhoto 方法希望传闭包参数回调拍照结果,正确应该怎么写。

(回调参数赋值给实例对象, 个人感觉是个很奇怪的做法)