Skip to content

Instantly share code, notes, and snippets.

View ThinhPhan's full-sized avatar
🎯
Focusing

Thinh Phan ThinhPhan

🎯
Focusing
View GitHub Profile
@ThinhPhan
ThinhPhan / enumerate-dictionary.m
Created June 30, 2015 19:26
Fast enumerate NSDictionary with keys in order.
Example:
Input:
@{
1 = "A";
2 = "B";
3 = "C";
}
Output
@[
@ThinhPhan
ThinhPhan / image-data-validation.m
Created July 8, 2015 17:13
Check image get from URL is valid or corrupted or URL return another type of data (JSON, garbage data ...)
- (void)isImageDataValid:(NSURL *)imageURL
{
dispatch_async(dispatch_queue_create("CheckImage", NULL), ^{
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:data];
if (image == nil) {
// imageURL is not valid
TLog(@"imageURL NOT valid: %@", imageURL);
}
@ThinhPhan
ThinhPhan / image-url-validation.m
Created July 8, 2015 17:17
Check from URL is a image URL or not by checking file's extension. You can check another file type: music or video ...
- (BOOL)isImageURLValid:(NSURL *)imageURL
{
NSArray *imageExtensions = @[@"jpg", @"jpeg", @"png", @"gif", @"tiff"]; // Add more if you want
NSString *extension = [imageURL pathExtension];
for (NSString *ext in imageExtensions) {
if ([ext isEqualToString:extension]) {
return YES;
}
}
@ThinhPhan
ThinhPhan / check-string-empty.m
Created August 18, 2015 10:35
Check string is 'empty'.
// 'Nothing' in Objective-C have a lot of meaning.
// Read [nil / Nil / NULL / NSNull] http://nshipster.com/nil/
// With string, nothing can be nil or "" - empty string.
// Bad
if ([string isEqualToString:@""] && password == nil) {
}
// Good - Simple check 'length' for both conditions above.
if ([string length] == 0) {
@ThinhPhan
ThinhPhan / custom-layer-corner.m
Created October 19, 2015 16:21
Custom corner: top edge, bottom edge, right edge or left edge
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:_quantityView.bounds
byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight) // Change your corner you want to round
cornerRadii:(CGSize){3.0f, 3.0f}].CGPath;
myView.layer.mask = maskLayer;
@ThinhPhan
ThinhPhan / ZoomToFitMapAnnotations.md
Last active August 21, 2024 15:00
Zoom out to fit all annotations on MapView

I search a lot of questions and answers in SO. This is what i figure out. The result I want is map can zoom out to fit all/some annotations INCLUDE user's location.

The basic steps are:

  • Calculate the min lat/long
  • Calculate the max lat/long
  • Create CLLocation objects for these two points
  • Calculate distance between points
  • Create region using center point between points and distance converted to degrees
@ThinhPhan
ThinhPhan / TextFieldAllowNumberOnly.m
Last active October 29, 2015 05:16
UITextField should allow only numbers.
#define ACCEPTABLE_CHARACTERS @"0123456789"
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (!string.length)
return YES;
if (textField == self.quantityTextField) {
NSCharacterSet *numbersOnly = [[NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARACTERS] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:numbersOnly] componentsJoinedByString:@""];
BOOL stringIsValid = [string isEqualToString:filtered];
@ThinhPhan
ThinhPhan / Instructions.md
Created October 17, 2017 10:24 — forked from mcfadden/Instructions.md
Making an IP camera with the Raspberry Pi Including on-board motion detection, and a password protected web server for viewing the camera stream

Making an IP camera with the Raspberry Pi

Including on-board motion detection

Requirements

  • Raspberry Pi (512MB rev2 recommended)
  • Raspberry Pi Camera board
  • SD Card (min 2BG, 8GB recommended for good measure. Class 10)

Optionally, a wifi adapter verified to work with raspberry pi ( I used Edimax Wireless Nano USB Adapter - http://www.amazon.com/gp/product/B005CLMJLU/ )

@ThinhPhan
ThinhPhan / Install Node Raspi3.md
Last active June 6, 2019 05:42
Install Node on Raspi3

Opt 1: From Nodesource

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs

Opt 2: Download & Install

wget https://nodejs.org/dist/v4.3.2/node-v4.3.2-linux-armv6l.tar.gz 
@ThinhPhan
ThinhPhan / Makefile
Created July 12, 2018 21:30
Setup Electron Kiosk App On Ubuntu 18.04
.PHONY: install uninstall test set-default
test:
sudo apt install plymouth-x11
sudo plymouthd ; sudo plymouth --show-splash ; for ((I=0; I<10; I++)); do sleep 1 ; sudo plymouth --update=test$I ; done ; sudo plymouth --quit
uninstall:
rm -rv /usr/share/plymouth/themes/first || true
install: uninstall