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 / 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 / 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 / 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 / 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 / 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 / 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 / UIImage+SimpleResize.h
Created June 18, 2015 08:07
UIImage category for resizing
// UIImage+SimpleResize.h
//
// Created by Robert Ryan on 5/19/11.
#import <Foundation/Foundation.h>
/** Image resizing category.
*
* Modified by Robert Ryan on 5/19/11.
*
@ThinhPhan
ThinhPhan / ConvertImageToBase64String.m
Last active July 22, 2023 07:16
Convert Image to Base64 string
// Ref: http://stackoverflow.com/questions/11251340/convert-image-to-base64-string-in-ios-swift
// iOS7 > version for Objective-C
// You can use NSData's base64EncodedStringWithOptions
// Now encode as:
- (NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
@ThinhPhan
ThinhPhan / SaveImageToFile.m
Last active August 29, 2015 14:23
Save image object to file.
// Reference links:
// https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/TechniquesforReadingandWritingCustomFiles/TechniquesforReadingandWritingCustomFiles.html
// http://stackoverflow.com/questions/7651632/how-to-speed-up-saving-a-uiimagepickercontroller-image-from-the-camera-to-the-fi
// http://www.codedisqus.com/0zJVUVqgPj/objectivec-uiimagewritetosavedphotosalbum-asynchronous-problems.html
// http://stackoverflow.com/questions/14411369/when-i-am-using-uiimagepngrepresentation-or-uiimagejpegrepresentation-for-conver?rq=1
// Save multi files async for saving time
- (void)saveUploadImages:(NSMutableArray *)images {
NSMutableArray *imagePaths = [[NSMutableArray alloc] init];
@ThinhPhan
ThinhPhan / CustomBackButton.m
Created June 12, 2015 09:58
Custom Back Button And Swipe Back. The only nice way I found to actually completely override the back button is to set the leftBarButtonItem, but then the swipe back gesture breaks. Luckily it's an easy fix when you know how
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithImage:img
style:UIBarButtonItemStylePlain
target:self
action:@selector(onBack:)];
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;