Skip to content

Instantly share code, notes, and snippets.

View phucnm's full-sized avatar
😬
eager to learn

Tony Nguyen phucnm

😬
eager to learn
  • Vancouver, BC, Canada
View GitHub Profile
@phucnm
phucnm / UIImage+Resize.h
Created December 10, 2015 03:25
Scale image in iOS without lose quality much - UIImage category
#import <UIKit/UIKit.h>
@interface UIImage (Resize)
- (UIImage*)scaleBy:(float)scale;
@end
@phucnm
phucnm / gist:4607206f16ad30bffc3c
Created December 10, 2015 03:31
Present UIImagePickerController (or any UIViewController) in iOS9 or later because of deprecated UIPopOverController
- (void)presentImagePicker:(UIView *)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.modalPresentationStyle = UIModalPresentationPopover;
imagePicker.popoverPresentationController.sourceView = sender;
imagePicker.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:imagePicker animated:YES completion:nil];
}
@phucnm
phucnm / gist:9a91fda097eebeb6df52
Last active December 10, 2015 03:36
Get thumnail of a video from a URL at arbitrary time
// import AVFoundation
- (UIImage*)thumbnailFromVideo:(NSURL *)url atTime:(float)time {
AVAsset *asset = [AVAsset assetWithURL:url];
// get 3 decimal places
// ex: 3.39235350 --> 3.392
float nearest = roundf(time * 1000) / 1000;
// make CMTime from float
// because of 3 decimal places, we use 10000 (10^4)
@phucnm
phucnm / gist:8e6b755f16761bae4c17
Created December 11, 2015 07:52
Modify Rakefile to build ffmpeg on iOS
kxmovie on github
First: cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/developer/SDKs/ to find out what SDKs are existed in your OS
Second: open Rake file and modify SDK_VERSION='9.2' -> your SDK version
@phucnm
phucnm / build-ffmpeg.sh
Created December 23, 2015 09:03 — forked from m1entus/build-ffmpeg.sh
Installing ffmpeg ios libraries armv7, armv7s, i386
#!/bin/bash
###########################################################################
# Choose your ffmpeg version and your currently-installed iOS SDK version:
#
VERSION="2.0.2"
SDKVERSION="7.0"
ARCHS="armv7 armv7s i386"
#
#
@phucnm
phucnm / gist:4fe443ee17a1e0773563
Created December 25, 2015 10:18
How to add FFmpeg to iOS project
Here are steps:
1. Build FFmpeg for iOS
There some scripts to build FFmpeg for iOS
ex: https://github.com/kewlbear/FFmpeg-iOS-build-script
2. After build complete, copy all *.a to your iOS project folder.
They will be automatically add to Linked Frameworks and Libraries (in Project settings -> General)
ex: libavutil.a, libavformat.a, libavcodec.a,...
3. Add depedencies to Linked Frameworks and Libraries
Add libz.dylib (or libz.tbd), libiconv.dylib
4. Go to Project settings -> Build Settings, add the path which contains all *.a files in your iOS project folder to Library Search Paths
@phucnm
phucnm / gist:fbabf2213809e11174c6
Last active January 7, 2016 03:02
Store and retrieve NSAttributedString or NSMutableAttributedString
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Hello, world!"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)];
[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24] range:NSMakeRange(3, 3)];
NSData *data = [string dataFromRange:NSMakeRange(0, string.length)
documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType,
NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
error:nil];
@phucnm
phucnm / gist:59599cf4bae05bed284b
Created February 29, 2016 05:06
Record video from camera with AVAssetWriter
Sometimes, for some reason, you have to use AVAssetWriter to record videos from camera instead of using movile file output.
I had experienced some issue by that recording method due to lacking of tutorial and SO questions :))
Noted for everyone who met the issue: black first frame
if (CMSampleBufferDataIsReady(sampleBuffer)) {
if (videoWriter.status != AVAssetWriterStatusWriting) {
CMTime startTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:startTime];
@phucnm
phucnm / gist:712faf4584c5baa9a84f
Created March 4, 2016 04:05
Get IP NSString from address data in iOS
#include <arpa/inet.h>
- (NSString *)getStringFromAddressData:(NSData *)dataIn {
struct sockaddr_in *socketAddress = nil;
NSString *ipString = nil;
socketAddress = (struct sockaddr_in *)[dataIn bytes];
ipString = [NSString stringWithFormat: @"%s",
inet_ntoa(socketAddress->sin_addr)]; ///problem here
return ipString;
}
@phucnm
phucnm / gist:4f68c8760ffe5ddc60b0
Created March 7, 2016 09:45
Play video with AVPlayer
NSURL *videoURL = [NSURL URLWithString:@"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];
NSURL *videoURL = [NSURL URLWithString:@"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = self.view.bounds;