我有一个带有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)
上面的代码打印到控制台: …
我正在使用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)