Skip to content

Instantly share code, notes, and snippets.

@shalyf
shalyf / cpu_usage.m
Last active July 11, 2017 07:17
CPU使用率
#import <mach/mach.h>
#import <sys/sysctl.h>
+ (double)cpuUsage {
kern_return_t kr;
task_info_data_t tinfo;
mach_msg_type_number_t task_info_count;
task_info_count = TASK_INFO_MAX;
kr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)tinfo, &task_info_count);
@shalyf
shalyf / NSNetService+ResolveTXTRecord.swift
Created August 3, 2017 09:05
解析 Bonjour 的 TXTRecord 数据
func resolveDictionary(fromTXTRecord data: Data) -> [String: String] {
var dict = [String: String]()
var offset = 0
let bytes = data.bytes
while offset < bytes.count {
let recordLength = Int(bytes[offset])
offset += 1
print(recordLength)
if recordLength > 0 {
let subBytes = bytes[offset..<offset + recordLength]
@shalyf
shalyf / hidesNavigationBarBottomLine.swift
Created August 16, 2017 11:39
隐藏 NavigationBar 底部的虚线
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
@shalyf
shalyf / livePhotoExportVideo.swift
Created August 24, 2017 06:54
Live Photo导出成视频
let resources = PHAssetResource.assetResources(for: asset)
let videoResource = resources.first(where: { $0.type == .video || $0.type == .pairedVideo })!
let url = URL(fileURLWithPath: FileManager.Folder.tmp.append(pathComponent: "test.MOV"))
let resourceOptions = PHAssetResourceRequestOptions()
resourceOptions.isNetworkAccessAllowed = true
PHAssetResourceManager.default().writeData(for: videoResource, toFile: url, options: resourceOptions, completionHandler: { (error) in
print(error == nil)
})
@shalyf
shalyf / reveal.swift
Created October 17, 2017 06:04
reveal command with xcode breakpoint
expr (Class)NSClassFromString(@"IBARevealLoader") == nil ? (void *)dlopen("/Applications/Reveal.app/Contents/SharedSupport/iOS-Libraries/RevealServer.framework/RevealServer", 0x2) : ((void*)0)
@shalyf
shalyf / yuv2rgb.mm
Last active October 12, 2023 18:33
create rgb image from yuv color space
#define clamp(a) (a>255?255:(a<0?0:a))
+ (UIImage *)imageFromSampleBuffer:(CMSampleBufferRef)sampleBuffer {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer, 0);
if (CVPixelBufferGetPlaneCount(imageBuffer) < 2) {
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
return nil;
}

启动屏中要添加图片经常会出现图片无法展示的问题
查了很多资料,最后得出一个对我来说有用的结论
1、将图片添加到根目录中
2、将图片添加到Assets.xcassets中
3、在LaunchScreen.storyboard中添加一个UIImageView,然后添加Assets.scassets中的图片即可

@shalyf
shalyf / black_color.swift
Created February 11, 2018 02:37
判断像素点是否是黑色
let isBlackColor = { (x: Int, y: Int) -> Bool in
let byteIndex = (bytesPerRow * y) + x * bytesPerPixel
let r = CGFloat(rawData[byteIndex])
let g = CGFloat(rawData[byteIndex + 1])
let b = CGFloat(rawData[byteIndex + 2])
// https://stackoverflow.com/a/12043228
let luma = 0.2126 * r + 0.7152 * g + 0.0722 * b
return luma < 40
}
@shalyf
shalyf / undefined_symbols.md
Last active July 9, 2020 08:17
Undefined symbols for architecture arm64

在Swift中使用Google Nearby API的时候,遇到了以下错误

Undefined symbols for architecture arm64:
"OBJC_CLASS$_GNSMessageManager", referenced from:
objc-class-ref in TBGNSMessager.o
"OBJC_CLASS$_GNSMessage", referenced from:
objc-class-ref in TBGNSMessager.o
@shalyf
shalyf / CVPixelBuffer(vImage).swift
Last active January 6, 2024 20:46
CVPixelBuffer通过vImage转换成CGImage
import Accelerate.vImage
func getImageBuffer(from pixelBuffer: CVPixelBuffer) -> vImage_Buffer? {
var buffer = vImage_Buffer()
let bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.byteOrder32Little.rawValue | CGImageAlphaInfo.first.rawValue)
var cgFormat = vImage_CGImageFormat(bitsPerComponent: 8,
bitsPerPixel: 32,
colorSpace: nil,
bitmapInfo: bitmapInfo,
version: 0,