Skip to content

Instantly share code, notes, and snippets.

@Anik0808
Anik0808 / readImageBuffer.swift
Last active February 12, 2024 06:19
This function reads a video and exposes it's pixel buffers.
func readImageBuffer(fromVideo videoURL: URL) {
let asset = AVAsset(url: videoURL)
let assetTrack = asset.tracks.first!
let assetReader = try! AVAssetReader(asset: asset)
let outputSettings: [String: Any] = [
String(kCVPixelBufferPixelFormatTypeKey): kCVPixelFormatType_32BGRA,
String(kCVPixelBufferWidthKey): 513,
String(kCVPixelBufferHeightKey): 513,
]
@Anik0808
Anik0808 / WritePixelBufferToFiles.swift
Created February 8, 2024 05:05
This code writes series of pixel buffer to files
//generate a file url to store the video. some_image.jpg becomes some_image.mov
guard let outputMovieURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("output.mov") else {
fatalError("somehting went wrong") //an error i made up
}
//delete any old file
do {
try FileManager.default.removeItem(at: outputMovieURL)
} catch {
print("Could not remove file \(error.localizedDescription)")
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// get the content offset for y-axis
let contentOffsetheight = scrollView.contentOffset.y
//if the value is negative
if contentOffsetheight < 0 {
//change the anchors
contentViewTopAnchor.constant = contentOffsetheight
headerViewHeightAnchor.constant = 450 + (-contentOffsetheight)
}
}
parentScrollView.delegate = self
func setupView(){
view.backgroundColor = .black
//setup the scrollView
view.addSubview(parentScrollView)
parentScrollView.fillSuperview()
parentScrollView.contentSize = CGSize(width: DeviceSize.width,
height: DeviceSize.height*2)
//setup the content view
parentScrollView.addSubview(contentView)
func setupView(){
view.backgroundColor = .black
//setup the scrollView
view.addSubview(parentScrollView)
parentScrollView.fillSuperview()
parentScrollView.contentSize = CGSize(width: DeviceSize.width,
height: DeviceSize.height*2)
//setup the content view
parentScrollView.addSubview(contentView)
var contentViewTopAnchor: NSLayoutConstraint!
var headerViewHeightAnchor: NSLayoutConstraint!
import UIKit
// Reference Video: https://youtu.be/iqpAP7s3b-8
struct AnchoredConstraints {
var top, leading, bottom, trailing, width, height: NSLayoutConstraint?
}
let DeviceSize = UIScreen.main.bounds.size
let parentScrollView: UIScrollView = {
let sv = UIScrollView()
sv.contentInsetAdjustmentBehavior = .never
return sv
}()
let contentView: UIView = {
let view = UIView()
return view
}()
struct HomeView: View {
@EnvironmentObject var subscription: Subscription
var body: some View {
VStack{
if subscription.isSubscribed{
Text("Welcome Premium User.")
}
else{
Text("Welcome to the App.")
}