Skip to content

Instantly share code, notes, and snippets.

View iAmrSalman's full-sized avatar

Amr Salman iAmrSalman

View GitHub Profile
@iAmrSalman
iAmrSalman / AVPlayer+Combine.swift
Created March 9, 2022 11:32 — forked from kshivang/AVPlayer+Combine.swift
Combine wrapper over AVPlayer.addPeriodicTimeObserver method. Use AVPlayer.periodicTimePublisher method instead.
import AVFoundation
import Combine
// simply use
// player.periodicTimePublisher()
// .receive(on: RunLoop.main)
// .assign(to: \SomeClass.elapsedTime, on: someInstance)
// .store(in: &cancelBag)
func attributedString(for type: DateType, with date: Date) -> NSAttributedString {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "ar_EG")
formatter.dateFormat = "dd\nMMM\nyyyy"
if type == .hijri {
formatter.calendar = Calendar(identifier: .islamicCivil)
}
var formattedDate = formatter.string(from: date)
formattedDate += "\(type == . hijri ? "هـ" : "م")"
let attributedString = NSMutableAttributedString(string: formattedDate, attributes: [
@iAmrSalman
iAmrSalman / swiftlint.sh
Created January 25, 2020 09:50 — forked from teameh/swiftlint.sh
Swiftlint pre-commit script
#!/bin/bash
set -e
if [ "$CI" == true ]; then
echo "CI == true, skipping this script"
exit 0
fi
SWIFT_LINT=./Pods/SwiftLint/swiftlint
class YourObjectVC: UIViewController {
var shareDelegate: ShareDelegate = ShareManager()
func onShareBtnPressed(_ sender: UIButton) {
shareDelegate.share("Hello, World!")
}
}
class ShareManager: ShareDelegate {
func share(_ message: String) { ... }
}
protocol ShareDelegate {
func share(_ message: String)
}
@iAmrSalman
iAmrSalman / Film.swift
Created August 28, 2018 09:56
[Film] Get all needed data to present a local saved video file (duration, thumbnail) #swift4 #avfoundation
import UIKit
import AVFoundation
class Film {
//MARK: - Properties
var name: String
var path: URL
var duration: String {
@iAmrSalman
iAmrSalman / encodeVideo.swift
Last active June 12, 2023 19:10
[Convert MOV to MP4] function to convert MOV to MP4 video format#swift #avkit #avasset
// Don't forget to import AVKit
func encodeVideo(at videoURL: URL, completionHandler: ((URL?, Error?) -> Void)?) {
let avAsset = AVURLAsset(url: videoURL, options: nil)
let startDate = Date()
//Create Export session
guard let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough) else {
completionHandler?(nil, nil)
return
@iAmrSalman
iAmrSalman / getVideoDuration.swift
Last active August 23, 2021 14:50
[Get video duration] function to get video duration #video
//Don't forget to import AVFoundation
func getVideoDuration(from path: URL) -> String {
let asset = AVURLAsset(url: path)
let duration: CMTime = asset.duration
let totalSeconds = CMTimeGetSeconds(duration)
let hours = Int(totalSeconds / 3600)
let minutes = Int((totalSeconds.truncatingRemainder(dividingBy: 3600)) / 60)
let seconds = Int(totalSeconds.truncatingRemainder(dividingBy: 60))
@iAmrSalman
iAmrSalman / getThumbnail.swift
Last active May 17, 2018 07:30
[Generate Video Thumbnail] function to generate video thumbnail using AVFoundation #avfoundation
//Don't forget to import AVFoundation
func getVideoThumbnail(from path: URL?) -> UIImage? {
guard let path = path else { return nil }
do {
let asset = AVURLAsset(url: path , options: nil)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)