小编hei*_*nia的帖子

如何复制UIImage?

我有一个带有UIImage的UIImageView.我想将这些图片的副本分配给两个变量.根据用户正在做的事情,应该操纵图像.问题是,每个变量的图像都是一样的.我想,因为它们是通过引用传递的.

let image1 = imageView.image!
let image2 = imageView.image!
Run Code Online (Sandbox Code Playgroud)

如何获得此图像的两个单独副本?

谢谢!

编辑:

我想要实现的目标:只裁剪一张图像,保持另一张图像与原始图像一样.

let imageLeft = googleImageView.image!
let imageRef = CGImageCreateCopy(googleImageView.image!.CGImage)
let imageRight = UIImage(CGImage: imageRef!, scale: googleImageView.image!.scale, orientation: googleImageView.image!.imageOrientation)

if translation.x < 0 {
    let scale = imageLeft.scale
    let newRect = CGRectMake(0, 0, (imageLeft.size.width + translation.x) * scale, imageLeft.size.height * scale)
    let imageRef = CGImageCreateWithImageInRect(imageLeft.CGImage, newRect)

    if let croppedImage = imageRef {
        googleImageView.image = UIImage(CGImage: croppedImage, scale: scale, orientation: imageLeft.imageOrientation)
    }
} 
print("left image: \(imageLeft) right image \(imageRight)")
Run Code Online (Sandbox Code Playgroud)

上面的代码打印到控制台: …

uiimage swift

13
推荐指数
2
解决办法
5852
查看次数

HealthKit - requestAuthorization(toShare:read:completion :)总是成功

我正在使用Xcode 8 beta 6,我正在请求访问Health App.requestAuthorization(toShare:read:completion:)请求授权的方法总是true在完成处理程序返回时产生- success在我的下面的代码中.即使我拒绝模拟器中的所有内容,我也会得到一个true.这是我处理授权的代码.我的代码中的某些内容是错误的还是这是一个Xcode错误?

import Foundation
import HealthKit

class HealthManager {
    private let healthStore = HKHealthStore()

    class var sharedInstance: HealthManager {
        struct Singleton {
            static let instance = HealthManager()
        }
        return Singleton.instance
    }

    private var isAuthorized: Bool? = false

    func authorizeHealthKit(completion: ((_ success: Bool) -> Void)!) {
        let writableTypes: Set<HKSampleType> = [HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.distanceWalkingRunning)!, HKWorkoutType.workoutType(), HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!, HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.activeEnergyBurned)!, HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!]
        let readableTypes: Set<HKSampleType> = [HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.distanceWalkingRunning)!, HKWorkoutType.workoutType(), HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!, HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.activeEnergyBurned)!, …
Run Code Online (Sandbox Code Playgroud)

xcode ios swift healthkit xcode8-beta6

5
推荐指数
1
解决办法
2520
查看次数

SwiftUI 检查器在项目中消失

在我的一个 Xcode 项目中,SwiftUI Inspector 将不再显示。

例子: 在此处输入图片说明

当我创建一个新项目时,我有检查员:

在此处输入图片说明

知道是什么原因造成的吗?我可以重置项目的设置吗?

xcode swiftui

2
推荐指数
1
解决办法
965
查看次数

标签 统计

swift ×2

xcode ×2

healthkit ×1

ios ×1

swiftui ×1

uiimage ×1

xcode8-beta6 ×1