Communitygithub.com

JordanCoin/ios-skills-collection

iOS 앱에서 Core ML 모델을 통합하고 최적화하여 온디바이스 머신러닝 추론을 수행합니다. 모델 로딩(.mlmodelc, .mlpackage), 자동 생성 클래스 및 MLFeatureProvider를 사용한 예측, 컴퓨트 유닛 구성(CPU, GPU, Neural Engine), MLTensor, VNCoreMLRequest, MLComputePlan, 다중 모델 파이프라인 및 배포 전략을 다룹니다. Core ML 모델 로딩, 예측 생성, 컴퓨트 유닛 구성 또는 모델 성능 프로파일링 시 사용하십시오.

ios-skills-collection란 무엇인가요?

이 스킬은 iOS 앱에서 Core ML 모델을 로드, 구성, 실행하는 방법을 포괄적으로 다룹니다. Swift 측면에 초점을 맞추어 모델 로딩, 예측, MLTensor, 프로파일링 및 배포를 포함하며, iOS 26+ 및 Swift 6.3을 대상으로 하고 명시된 경우를 제외하고 iOS 14까지 하위 호환됩니다. 주요 내용으로는 자동 생성 클래스 및 수동 URL 로딩을 통한 모델 로딩, MLModelConfiguration을 사용한 컴퓨트 유닛(CPU, GPU, Neural Engine) 구성, MLDictionaryFeatureProvider 또는 자동 생성 클래스를 사용한 동기 및 비동기 예측, batch prediction, iOS 18+의 stateful prediction, iOS 18+의 MLTensor를 사용한 Swift 네이티브 다차원 배열 처리, MLMultiArray를 사용한 데이터 교환, 이미지 전처리, 다중 모델 파이프라인, Vision 통합(VNCoreMLRequest 및 CoreMLRequest), MLComputePlan을 사용한 성능 프로파일링, 번들 및 온디맨드 리소스를 사용한 배포 전략, 메모리 관리 모범 사례가 포함됩니다. 이 스킬은 Core ML을 사용하는 iOS 개발자에게 필수적이며, Python 측 모델 변환 및 최적화는 별도의 apple-on-device-ai 스킬에서 다룹니다.

지원 대상~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/JordanCoin/ios-skills-collection/tree/HEAD/skills/dpearson2699--coreml

즐겨 사용하는 AI에게 물어보기

이 에이전트 스킬이 미리 로드된 새 채팅을 엽니다.

문서

Core ML Swift 통합

iOS 앱에서 Core ML 모델을 로드, 구성 및 실행합니다. 이 스킬은 Swift 측면(모델 로딩, 예측, MLTensor, 프로파일링, 배포)을 다룹니다. 명시된 경우를 제외하고 iOS 26+ 및 Swift 6.3을 대상으로 하며, iOS 14까지 하위 호환됩니다.

범위 경계: Python 측의 모델 변환, 최적화(양자화, 팔레타이제이션, 가지치기) 및 프레임워크 선택은 apple-on-device-ai 스킬에서 다룹니다. 이 스킬은 Swift 통합만을 전담합니다.

전체 코드 패턴(액터 기반 캐싱, 배치 추론, 이미지 전처리, 테스트 포함)은 references/coreml-swift-integration.md를 참조하십시오.

목차

모델 로딩

자동 생성 클래스

.mlpackage 또는 .mlmodelc를 Xcode로 드래그하면 입력/출력이 타입화된 Swift 클래스가 생성됩니다. 가능하면 항상 이를 사용하십시오.

import CoreML

let config = MLModelConfiguration()
config.computeUnits = .all

let model = try MyImageClassifier(configuration: config)

수동 로딩

런타임에 모델이 다운로드되거나 번들 외부에 저장된 경우 URL에서 로드합니다.

let modelURL = Bundle.main.url(
    forResource: "MyModel", withExtension: "mlmodelc"
)!
let model = try MLModel(contentsOf: modelURL, configuration: config)

비동기 로딩 (iOS 16+)

메인 스레드를 차단하지 않고 모델을 로드합니다. 큰 모델의 경우 이 방식을 선호하십시오.

let model = try await MLModel.load(
    contentsOf: modelURL,
    configuration: config
)

런타임 컴파일

기기에서 .mlpackage 또는 .mlmodel을 .mlmodelc로 컴파일합니다. 서버에서 다운로드된 모델에 유용합니다.

let compiledURL = try await MLModel.compileModel(at: packageURL)
let model = try MLModel(contentsOf: compiledURL, configuration: config)

컴파일된 URL을 캐싱하십시오. 매번 실행할 때마다 다시 컴파일하는 것은 시간 낭비입니다. compiledURL을 영구적인 위치(예: Application Support)로 복사하십시오.

모델 구성

MLModelConfiguration은 컴퓨트 유닛, GPU 액세스 및 모델 매개변수를 제어합니다.

컴퓨트 유닛 결정표

값사용선택 시기
.allCPU + GPU + Neural Engine기본값. 시스템이 결정하도록 합니다.
.cpuOnlyCPU백그라운드 작업, 오디오 세션, 또는 GPU가 사용 중일 때.
.cpuAndGPUCPU + GPUGPU가 필요하지만 모델에 ANE에서 지원되지 않는 연산이 있는 경우.
.cpuAndNeuralEngineCPU + Neural Engine호환되는 모델에 가장 좋은 에너지 효율.
let config = MLModelConfiguration()
config.computeUnits = .cpuAndNeuralEngine

// 낮은 우선순위 백그라운드 추론 허용
config.computeUnits = .cpuOnly

구성 속성

let config = MLModelConfiguration()
config.computeUnits = .all
config.allowLowPrecisionAccumulationOnGPU = true // 더 빠름, 약간의 정밀도 손실

예측 생성

자동 생성 클래스 사용

생성된 클래스는 타입화된 입력/출력 구조체를 제공합니다.

let model = try MyImageClassifier(configuration: config)
let input = MyImageClassifierInput(image: pixelBuffer)
let output = try model.prediction(input: input)
print(output.classLabel)        // "golden_retriever"
print(output.classLabelProbs)   // ["golden_retriever": 0.95, ...]

MLDictionaryFeatureProvider 사용

입력이 동적이거나 컴파일 시간에 알 수 없는 경우 사용합니다.

let inputFeatures = try MLDictionaryFeatureProvider(dictionary: [
    "image": MLFeatureValue(pixelBuffer: pixelBuffer),
    "confidence_threshold": MLFeatureValue(double: 0.5),
])
let output = try model.prediction(from: inputFeatures)
let label = output.featureValue(for: "classLabel")?.stringValue

비동기 예측 (iOS 17+)

let output = try await model.prediction(from: inputFeatures)

배치 예측

더 나은 처리량을 위해 한 번의 호출로 여러 입력을 처리합니다.

let batchInputs = try MLArrayBatchProvider(array: inputs.map { input in
    try MLDictionaryFeatureProvider(dictionary: ["image": MLFeatureValue(pixelBuffer: input)])
})
let batchOutput = try model.predictions(from: batchInputs)
for i in 0..<batchOutput.count {
    let result = batchOutput.features(at: i)
    print(result.featureValue(for: "classLabel")?.stringValue ?? "unknown")
}

상태 유지 예측 (iOS 18+)

예측 간 상태를 유지하는 모델(시퀀스 모델, LLM, 오디오 누적기)에 MLState를 사용합니다. 상태를 한 번 생성하고 각 예측 호출에 전달합니다.

let state = model.makeState()

// 각 예측이 내부 모델 상태를 전달
for frame in audioFrames {
    let input = try MLDictionaryFeatureProvider(dictionary: [
        "audio_features": MLFeatureValue(multiArray: frame)
    ])
    let output = try await model.prediction(from: input, using: state)
    let classification = output.featureValue(for: "label")?.stringValue
}

State는 Sendable이 아닙니다. 단일 액터 또는 태스크에서 사용하십시오. model.makeState()를 호출하여 동시 스트림에 대한 독립적인 상태를 생성합니다.

MLTensor (iOS 18+)

MLTensor는 전처리/후처리를 위한 Swift 네이티브 다차원 배열입니다. 연산은 지연 실행되며 결과를 구체화하려면 .shapedArray(of:)를 호출하십시오.

import CoreML

// 생성
let tensor = MLTensor([1.0, 2.0, 3.0, 4.0])
let zeros = MLTensor(zeros: [3, 224, 224], scalarType: Float.self)

// 형태 변경
let reshaped = tensor.reshaped(to: [2, 2])

// 수학 연산
let softmaxed = tensor.softmax()
let normalized = (tensor - tensor.mean()) / tensor.standardDeviation()

// MLMultiArray와 상호 운용
let multiArray = try MLMultiArray([1.0, 2.0, 3.0, 4.0])
let fromMultiArray = MLTensor(multiArray)
let backToArray = tensor.shapedArray(of: Float.self)

MLMultiArray 작업

MLMultiArray는 이미지가 아닌 모델 입력 및 출력에 대한 기본 데이터 교환 유형입니다. 자동 생성 클래스가 배열 유형의 특징을 예상할 때 사용하십시오.

// 3D 배열 생성: [batch, sequence, features]
let array = try MLMultiArray(shape: [1, 128, 768], dataType: .float32)

// 값 쓰기
for i in 0..<128 {
    array[[0, i, 0] as [NSNumber]] = NSNumber(value: Float(i))
}

// 값 읽기
let value = array[[0, 0, 0] as [NSNumber]].floatValue

// 제로-카피 상호 운용을 위한 데이터 포인터에서 생성
let data: [Float] = [1.0, 2.0, 3.0]
let fromData = try MLMultiArray(dataPointer: UnsafeMutableRawPointer(mutating: data),
                                 shape: [3],
                                 dataType: .float32,
                                 strides: [1])

NLP 토큰화 및 오디오 특징 추출을 포함한 고급 MLMultiArray 패턴은 references/coreml-swift-integration.md를 참조하십시오.

이미지 전처리

이미지 모델은 CVPixelBuffer 입력을 기대합니다. 카메라 또는 사진 라이브러리의 사진에는 CGImage 변환을 사용하십시오. Vision의 VNCoreMLRequest는 이를 자동으로 처리합니다. 수동 변환은 직접 MLModel 예측에만 필요합니다.

import CoreVideo

func createPixelBuffer(from cgImage: CGImage, width: Int, height: Int) -> CVPixelBuffer? {
    var pixelBuffer: CVPixelBuffer?
    let attrs: [CFString: Any] = [
        kCVPixelBufferCGImageCompatibilityKey: true,
        kCVPixelBufferCGBitmapContextCompatibilityKey: true,
    ]
    CVPixelBufferCreate(kCFAllocatorDefault, width, height,
                        kCVPixelFormatType_32ARGB, attrs as CFDictionary, &pixelBuffer)

    guard let buffer = pixelBuffer else { return nil }
    CVPixelBufferLockBaseAddress(buffer, [])
    let context = CGContext(
        data: CVPixelBufferGetBaseAddress(buffer),
        width: width, height: height,
        bitsPerComponent: 8, bytesPerRow: CVPixelBufferGetBytesPerRow(buffer),
        space: CGColorSpaceCreateDeviceRGB(),
        bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue
    )
    context?.draw(cgImage, in: CGRect(x: 0, y: 0, width: width, height: height))
    CVPixelBufferUnlockBaseAddress(buffer, [])
    return buffer
}

정규화, 중앙 자르기와 같은 추가 전처리 패턴은 references/coreml-swift-integration.md를 참조하십시오.

다중 모델 파이프라인

전처리 또는 후처리에 별도의 모델이 필요한 경우 모델을 연결합니다.

// 순차 추론: 전처리기 -> 주 모델 -> 후처리기
let preprocessed = try preprocessor.prediction(from: rawInput)
let mainOutput = try mainModel.prediction(from: preprocessed)
let finalOutput = try postprocessor.prediction(from: mainOutput)

Xcode 관리 파이프라인의 경우, .mlpackage의 파이프라인 모델 유형을 사용하십시오. 각 하위 모델은 최적의 컴퓨트 유닛에서 실행됩니다.

Vision 통합

Vision을 사용하여 자동 이미지 전처리(크기 조정, 정규화, 색 공간, 방향)와 함께 Core ML 이미지 모델을 실행합니다.

최신: CoreMLRequest (iOS 18+)

import Vision
import CoreML

let model = try MLModel(contentsOf: modelURL, configuration: config)
let request = CoreMLRequest(model: .init(model))
let results = try await request.perform(on: cgImage)

if let classification = results.first as? ClassificationObservation {
    print("\(classification.identifier): \(classification.confidence)")
}

레거시: VNCoreMLRequest

let vnModel = try VNCoreMLModel(for: model)
let request = VNCoreMLRequest(model: vnModel) { request, error in
    guard let results = request.results as? [VNRecognizedObjectObservation] else { return }
    for observation in results {
        let label = observation.labels.first?.identifier ?? "unknown"
        let confidence = observation.labels.first?.confidence ?? 0
        let boundingBox = observation.boundingBox // 정규화된 좌표
        print("\(label): \(confidence) at \(boundingBox)")
    }
}
request.imageCropAndScaleOption = .scaleFill

let handler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer)
try handler.perform([request])

완전한 Vision 프레임워크 패턴(텍스트 인식, 바코드 감지, 문서 스캐닝)은 vision-framework 스킬을 참조하십시오.

성능 프로파일링

MLComputePlan (iOS 17.4+)

예측을 실행하기 전에 각 연산이 어떤 컴퓨트 장치를 사용할지 검사합니다.

let computePlan = try await MLComputePlan.load(
    contentsOf: modelURL, configuration: config
)
guard case let .program(program) = computePlan.modelStructure else { return }
guard let mainFunction = program.functions["main"] else { return }

for operation in mainFunction.block.operations {
    let deviceUsage = computePlan.deviceUsage(for: operation)
    let estimatedCost = computePlan.estimatedCost(of: operation)
    print("\(operation.operatorName): \(deviceUsage?.preferredComputeDevice ?? "unknown")")
}

Instruments

Instruments의 Core ML 계측 템플릿을 사용하여 프로파일링하십시오:

  • 모델 로드 시간
  • 예측 지연 시간(연산별 분석)
  • 컴퓨트 장치 디스패치(연산별 CPU/GPU/ANE)
  • 메모리 할당

정확한 결과를 위해 디버거 외부에서 실행하십시오(Xcode: Product > Profile).

모델 배포

번들 vs 온디맨드 리소스

전략장점단점
앱 번들즉시 사용 가능, 오프라인 작동앱 다운로드 크기 증가
온디맨드 리소스초기 다운로드 크기 감소첫 사용 전 다운로드 필요
백그라운드 에셋 (iOS 16+)미리 다운로드설정이 더 복잡함
CloudKit / 서버최대 유연성네트워크 필요, 설정 시간 증가

크기 고려 사항

  • App Store 제한: 앱 번들 4 GB
  • 셀룰러 다운로드 제한: 200 MB (예외 요청 가능)
  • 50 MB 이상의 모델에는 ODR 태그 사용
  • 기기에서 컴파일을 건너뛰려면 미리 .mlmodelc로 컴파일
// 온디맨드 리소스 로딩
let request = NSBundleResourceRequest(tags: ["ml-model-v2"])
try await request.beginAccessingResources()
let modelURL = Bundle.main.url(forResource: "LargeModel", withExtension: "mlmodelc")!
let model = try await MLModel.load(contentsOf: modelURL, configuration: config)
// 완료되면 request.endAccessingResources() 호출

메모리 관리

  • 백그라운드에서 언로드: 앱이 백그라운드로 진입할 때 GPU/ANE 메모리를 확보하기 위해 모델 참조를 해제하십시오. 포그라운드로 돌아오면 다시 로드합니다.
  • 백그라운드 작업에는 .cpuOnly 사용: 백그라운드 처리는 GPU 또는 ANE를 사용할 수 없습니다. .cpuOnly를 설정하면 자동 대체 및 리소스 경합을 피할 수 있습니다.
  • 모델 인스턴스 공유: 동일한 컴파일된 모델에서 여러 MLModel 인스턴스를 절대 생성하지 마십시오. 액터를 사용하여 공유 액세스를 제공하십시오.
  • 메모리 압력 모니터링: 큰 모델(>100MB)은 메모리 경고를 트리거할 수 있습니다. UIApplication.didReceiveMemoryWarningNotification에 등록하고 압력이 있을 때 캐시된 모델을 해제하십시오.

액터 기반 모델 관리자(수명 주기 인식 로딩 및 캐시 제거 포함)는 references/coreml-swift-integration.md를 참조하십시오.

일반적인 실수

하지 말아야 할 것: 메인 스레드에서 모델을 로드합니다. 해야 할 것: MLModel.load(contentsOf:configuration:) 비동기 API를 사용하거나 백그라운드 액터에서 로드합니다. 이유: 큰 모델은 로드하는 데 몇 초가 걸릴 수 있어 UI가 멈출 수 있습니다.

하지 말아야 할 것: 앱 실행 시마다 .mlpackage를 .mlmodelc로 다시 컴파일합니다. 해야 할 것: MLModel.compileModel(at:)으로 한 번 컴파일하고 컴파일된 URL을 영구적으로 캐싱합니다. 이유: 컴파일은 비용이 많이 듭니다. Application Support에 .mlmodelc를 캐싱하십시오.

하지 말아야 할 것: 특별한 이유 없이 .cpuOnly를 하드코딩합니다. 해야 할 것: .all을 사용하고 시스템이 최적의 컴퓨트 유닛을 선택하도록 합니다.

[번역을 위해 잘림]

Individual skills in this repo

This repo contains 11 individual skills — each has its own dedicated page.

JordanCoin/ios-skills-collection

Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries. Use when the user asks about GitHub issues, pull requests, workflows, or wants to interact with GitHub repositories from the command line — including tasks like check CI status, create PR, list issues, or query the GitHub API.

JordanCoin/ios-skills-collection

Create media playback experiences using AVKit. Use when adding video players with AVPlayerViewController, enabling Picture-in-Picture, routing media with AirPlay, using SwiftUI VideoPlayer views, configuring transport controls, displaying subtitles and closed captions, or integrating AVFoundation playback with system UI.

JordanCoin/ios-skills-collection

Implement, review, or improve maps and location features in iOS/macOS apps using MapKit and CoreLocation. Use when working with Map views, annotations, markers, polylines, user location tracking, geocoding, reverse geocoding, search/autocomplete, directions and routes, geofencing, region monitoring, CLLocationUpdate async streams, or location authorization flows. Also use when working with maps, coordinates, addresses, places, directions, distance calculations, or location-based features in Swift apps.

JordanCoin/ios-skills-collection

Display and manipulate PDF documents using PDFKit. Use when embedding PDFView to show PDF files, creating or modifying PDFDocument instances, adding annotations (highlights, notes, signatures), extracting text with PDFSelection, navigating pages, generating thumbnails, filling PDF forms, or wrapping PDFView in SwiftUI.

JordanCoin/ios-skills-collection

Implement, review, or improve in-app tips and onboarding using Apple's TipKit framework. Use when adding feature discovery tooltips, onboarding flows, contextual tips, first-run experiences, coach marks, or working with Tip protocol, TipView, popoverTip, tip rules, tip events, or feature education UI.

JordanCoin/ios-skills-collection

When the user wants to optimize their Google Play Store listing — title, short description, full description, keywords, ratings, or Play Store-specific features. Use when the user mentions "Google Play", "Android", "Play Store", "Play Console", "short description", "full description indexed", "Google Play ASO", or wants Google Play-specific keyword, creative, or ratings strategy. For iOS App Store optimization, see aso-audit and metadata-optimization.

JordanCoin/ios-skills-collection

When the user wants to implement, optimize, or use App Clips for app discovery and conversion. Use when the user mentions "App Clip", "app clip code", "mini app", "instant app", "App Clip card", "App Clip link", "no download required", "instant experience", or wants to understand how App Clips appear in App Store search. For general App Store discoverability, see aso-audit. For marketing campaigns, see ua-campaign.

JordanCoin/ios-skills-collection

When the user wants to plan a launch strategy for a new app or major update. Also use when the user mentions "app launch", "launch plan", "launch checklist", "pre-launch", "launch day", or "how to launch my app". For ongoing ASO after launch, see aso-audit. For paid acquisition during launch, see ua-campaign.

JordanCoin/ios-skills-collection

When the user wants to analyze their own app's actual performance data from App Store Connect — real downloads, revenue, IAP, subscriptions, trials, or country breakdowns synced via Appeeky Connect. Use when the user asks about "my downloads", "my revenue", "how is my app performing", "ASC data", "sales and trends", "my subscription numbers", "App Store Connect metrics", or wants to compare periods or top markets. For third-party app estimates, see app-analytics. For subscription analytics depth, see monetization-strategy.

JordanCoin/ios-skills-collection

When the user wants a full ASO health audit, review their App Store listing quality, or diagnose why their app isn't ranking. Also use when the user mentions "ASO audit", "ASO score", "why am I not ranking", "listing review", or "optimize my app store page". For keyword-specific research, see keyword-research. For metadata writing, see metadata-optimization.

JordanCoin/ios-skills-collection

Always use when working on any iOS, macOS, Swift, SwiftUI, Xcode, or Apple platform project. Routes to the correct specialized skills based on development phase and task context. This collection contains 210 skills from 28 authors covering the full iOS lifecycle.

관련 스킬