PhotoEditKit

快速上手

为您的应用加入自动修图

安装库,传入照片,即可得到它需要的修图。全部在设备端完成:无需账号、无需 API 密钥、没有网络请求。

iOS与macOS

iOS 17.6+macOS 15+Swift Package Manager
  1. 第 1 步

    用 Swift Package Manager 安装

    在 Xcode 中选择 File → Add Package Dependencies…,输入 https://github.com/8kdesign/PhotoEditKit,并将 PhotoEditKit 库添加到应用目标。也可以在自己的 Package.swift 中声明:

    .package(url: "https://github.com/8kdesign/PhotoEditKit", from: "1.2.7")
  2. 第 2 步

    让 AI 修一张照片

    传给 AI 一张方向正确的小图(512 像素足够)以及照片的 ImageIO 属性。您会得到一个 EditConfig,用于渲染原尺寸照片;如果照片已经很好,则返回 nil。请在主线程以外运行。

    import ImageIO
    import PhotoEditKit
    
    guard let source = CGImageSourceCreateWithURL(photoURL as CFURL, nil) else { return }
    let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [String: Any]
    
    // A small upright copy is all the AI needs, and much faster than full size.
    let options = [
        kCGImageSourceCreateThumbnailFromImageAlways: true,
        kCGImageSourceCreateThumbnailWithTransform: true,
        kCGImageSourceThumbnailMaxPixelSize: 512,
    ] as CFDictionary
    guard let small = CGImageSourceCreateThumbnailAtIndex(source, 0, options) else { return }
    
    // Off the main thread: this measures the photo.
    if let config = AIPostProcessingHelper.topSuggestion(for: small, metadata: properties),
       let original = CIImage(contentsOf: photoURL, options: [.applyOrientationProperty: true]) {
        let edited: CIImage = original.applyingEditConfig(config)
    } else {
        // The photo is already good. Keep it as it is.
    }

    请传入原始文件的属性。借助 EXIF 曝光信息,AI 能分辨夜景街道和曝光不足的照片,不会把有意拍暗的照片调亮。

  3. 第 3 步

    展示修图建议选择器

    想让用户自己选?最多可获取十条排好序的建议,每条都有标题、EditConfig 和现成的缩略图。请传入原尺寸照片,以便测量噪点。

    // Off the main thread, with the full-size photo.
    let suggestions = AIPostProcessingHelper.suggestions(for: uiImage, metadata: properties)
    for suggestion in suggestions {
        suggestion.title      // e.g. "Brighten"
        suggestion.config     // EditConfig to apply
        suggestion.thumbnail  // small rendered preview, ready for a picker
    }

    在 macOS 上 API 完全相同:示例中使用 UIImage 的地方改用 NSImage 即可。

  4. 第 4 步

    阅读完整指南

    README 涵盖其余内容:用 EditController 构建编辑器、HDR 导出、实况照片、视频编辑以及所有设置。

安卓

Android 10+arm64-v8acompileSdk 36Kotlin 2.2+
  1. 第 1 步

    添加 Maven 仓库

    PhotoEditKit 发布在托管于 GitHub 的 Maven 仓库中。请在 settings.gradle.kts 中添加:

    dependencyResolutionManagement {
        repositories {
            google()
            mavenCentral()
            maven("https://raw.githubusercontent.com/8kdesign/PhotoEditKit-Android/main/maven") {
                content { includeGroup("com.logicui") }
            }
        }
    }
  2. 第 2 步

    添加依赖

    然后在应用模块的 build.gradle.kts 中添加该库。AI 引擎仅支持 64 位 ARM,因此请将应用限定为 arm64-v8a:

    android {
        defaultConfig {
            ndk { abiFilters += "arm64-v8a" }
        }
    }
    
    dependencies {
        implementation("com.logicui:photoeditkit:1.2.7")
    }
  3. 第 3 步

    让 AI 修一张照片

    用 ImageDecoder 解码出方向正确的照片,并传入它的 ExifInterface。suggestions 是在后台运行的挂起函数。在主线程以外用 renderedEdit 渲染最佳建议。

    import android.graphics.ImageDecoder
    import androidx.exifinterface.media.ExifInterface
    import com.logicui.photoeditkit.PhotoEditKit
    
    suspend fun autoEdit(context: Context, uri: Uri): Bitmap {
        val resolver = context.contentResolver
        // ImageDecoder applies the EXIF orientation, so the bitmap is upright.
        val photo = ImageDecoder.decodeBitmap(ImageDecoder.createSource(resolver, uri))
        val exif = resolver.openInputStream(uri)?.use { ExifInterface(it) }
    
        val result = PhotoEditKit.suggestions(context, photo, exif)  // runs in the background
        val best = result.suggestions.firstOrNull() ?: return photo
        return withContext(Dispatchers.Default) { PhotoEditKit.renderedEdit(photo, best.config) }
    }
  4. 第 4 步

    展示修图建议选择器

    每条建议都带有设备语言的标题、EditConfig 和 240 像素的缩略图,可直接用于选择器。构图均衡的照片可能不会返回任何建议。

    val result: SuggestionResult = PhotoEditKit.suggestions(context, photo, exif)
    for (suggestion in result.suggestions) {
        suggestion.title      // "Brighten", in the device's language
        suggestion.config     // EditConfig to apply
        suggestion.thumbnail  // 240 px rendered preview, ready for a picker
    }
  5. 第 5 步

    阅读完整指南

    README 涵盖其余内容:用 EditPreviewView 预览、Ultra HDR、动态照片、视频编辑以及所有设置。