我想从当前的图形上下文创建一个UIImage对象.更具体地说,我的用例是用户可以绘制线条的视图.他们可以逐步绘制.完成后,我想创建一个UIImage来代表他们的绘图.
这是drawRect:现在对我来说是这样的:
- (void)drawRect:(CGRect)rect
{
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSaveGState(c);
CGContextSetStrokeColorWithColor(c, [UIColor blackColor].CGColor);
CGContextSetLineWidth(c,1.5f);
for(CFIndex i = 0; i < CFArrayGetCount(_pathArray); i++)
{
CGPathRef path = CFArrayGetValueAtIndex(_pathArray, i);
CGContextAddPath(c, path);
}
CGContextStrokePath(c);
CGContextRestoreGState(c);
}
Run Code Online (Sandbox Code Playgroud)
...其中_pathArray的类型为CFArrayRef,并且每次调用touchesEnded:时都会填充.另请注意,drawRect:可以在用户绘制时多次调用.
用户完成后,我想创建一个表示图形上下文的UIImage对象.有关如何做到这一点的任何建议?
我正在尝试实时访问 Apple Watch 记录的心跳数据。通过 HKWorkoutSession,我可以每 5 秒回调一次workoutBuilder didCollectDataOf心率(心跳/分钟)数据。
这对于我的应用程序来说不够实时,因此我正在考虑使用新的(从 iOS 13 和 watchOS 6.0 开始)HKHeartbeatSeriesSample(和相关类)来实现此目的。我的理解是,心跳数据的系列样本将以系列形式记录逐次心跳的测量结果。
\n\n我能够让 API 工作并获取一些数据,但不是实时的——我获取的系列数据来自以前的一些记录(不清楚是什么触发了这些记录)。
\n\n这里是授权:
\n\n let allTypes: Set<HKSampleType> = Set([\n HKObjectType.workoutType(),\n HKSeriesType.heartbeat(),\n HKObjectType.quantityType(forIdentifier: .heartRate)!,\n HKQuantityType.quantityType(forIdentifier: .heartRateVariabilitySDNN)!,\n ])\n\n healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in\n ///...\n let workoutSession = WorkoutSession(healthStore: self.healthStore)\n workoutSession.startHeartbeatSampleQuery()\n }\n\nRun Code Online (Sandbox Code Playgroud)\n\nstartHeartBeatSampleQuery:
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate,\n ascending: false)\n\n // Query for the heartbeat samples from the specified heartbeat series.\n let heartbeatSeriesSampleQuery = HKSampleQuery(sampleType: HKSeriesType.heartbeat(),\n …Run Code Online (Sandbox Code Playgroud)