What are the common algorithms and application knowledge points of iOS
This article mainly introduces the common iOS algorithms and application knowledge points, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Algorithm comparison
Keywords
Bisection, recursion, divide and conquer backtracking
Bubbling sort
Idea: two loops, the outer layer controls the number of cycles, the inner layer loops, the comparison between data, large data floating (sinking)
# pragma mark-Objective-C// bubble sort-(void) bubbleSort: (id) array {if (! ([array isKindOfClass: [NSArray class]] | | [array isKindOfClass: [NSMutableArray class]]) {NSLog (@ "the parameter passed in is not an array type"); return;} NSMutableArray * tmpArr; if ([array isKindOfClass: [NSMutableArray class]) {tmpArr = array;} else {tmpArr = [array mutableCopy];} for (int I = 0; I = high) {return } / / take the median value int middle = low + (high-low) / 2; NSNumber * prmt = array [middle]; int I = low; int j = high; / / start sorting such that leftprmt while (I [prmt intValue]) {jmurt;} if (I) {[self quickSort:array low:i high:high];} / / Quick sort int a [101], n / / define global variables. These two variables need to be used in subfunctions using void quicksort (int left,int right) {int iRecitemp; if (left > right) return; temp=a [left]; / / what is stored in temp is the cardinal number itransift; jobright.global variables {/ / order is very important. Start with while (AJ] > = temp & item1) {dk = dk/2; [self ShellSort:array dk:dk];}} in the subfunction
Practical application
Compress a picture
+ (NSData *) compressImage: (UIImage *) image toByte: (NSUInteger) maxLength {/ / Compress by quality CGFloat compression = 1; NSData * data = UIImageJPEGRepresentation (image, compression); if (data.length
< maxLength) return data; //采用二分法提高性能 CGFloat max = 1; CGFloat min = 0; for (int i = 0; i < 6; ++i) { compression = (max + min) / 2; data = UIImageJPEGRepresentation(image, compression); if (data.length < maxLength * 0.9) { min = compression; } else if (data.length >MaxLength) {max = compression;} else {break;}} UIImage * resultImage = [UIImage imageWithData:data]; if (data.length
< maxLength) return data; // Compress by size NSUInteger lastDataLength = 0; while (data.length >MaxLength & & data.length! = lastDataLength) {lastDataLength = data.length; CGFloat ratio = (CGFloat) maxLength / data.length; CGSize size = CGSizeMake ((NSUInteger) (resultImage.size.width * sqrtf (ratio)), (NSUInteger) (resultImage.size.height * sqrtf (ratio); / / Use NSUInteger to prevent white blank UIGraphicsBeginImageContext (size); [resultImage drawInRect:CGRectMake (0,0, size.width, size.height)]; resultImage = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); data = UIImageJPEGRepresentation (resultImage, compression);} return data } + (NSData *) compressImage: (UIImage *) image {NSData * data=UIImageJPEGRepresentation (image, 1. 0); if (data.length > 300 / 1024) {if (data.length > 1024 / 1024) {/ / 1m and above data=UIImageJPEGRepresentation (image, 0.5);} else if (data.length > 300 / 1024) {/ / 0.5M-1M data=UIImageJPEGRepresentation (image, 0.8);}} return data;}
Thank you for reading this article carefully. I hope the article "what are the common algorithms and application knowledge points of iOS" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!