快速連結

2011年12月20日

筆記:xcode小功能(?)

不過原本文章撰寫是:开发小问题 - Ghosthkfly

不確定XCode的版本。



將NSString去掉前後空格:
[NSString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

獲取顏色的數值:

    UIColor *uicolor = [UIColor redColor];
    CGColorRef color = [uicolor CGColor];
    int numComponents = CGColorGetNumberOfComponents(color);
    if (numComponents >= 3){
        const CGFloat *tmComponents = CGColorGetComponents(color);
        red = tmComponents[0];
        green = tmComponents[1];
        blue = tmComponents[2];
        alpha = tmComponents[3];
    }

獲取0~1間的隨機數值:

(float)(1+arc4random()% 99)/100

讀取一個URL傳遞的值:

- (NSString *) urlString:(NSString *)value{
      NSURL *url = [NSURL URLWithString:value];
      NSData *data = [NSData dataWithContentsOfURL:url];
      //此處使用UTF8,如果要用big5則改成:kCFStringEncodingBig5
      NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF8);
      NSString *retStr = [[NSString alloc] initWithData:data encoding:enc];
      return retStr;
}

NSArray的比較排序:
附註~由于此方法是c方法,所以应该写再@end后面或者@implementation前面。
如果写在@implementation前面的话,就不需要声明了,如果写在@end前面的话,得在头文件声明。或者@implementation前面声明此方法。

NSInteger intSort(id num1, id num2, void *context){
  int v1 = [num1 intValue];
  int v2 = [num2 intValue];
  if (v1 < v2)
     return NSOrderedAscending;
  else if (v1 > v2)
     return NSOrderedDescending;
  else
     return NSOrderedSame;
}
要使用的時候這樣子使用:

NSArray *sortedArray;
sortedArray = [anArray sortedArrayUsingFunction:intSort context:NULL];

創立一個plist文件:

     NSMutableDictionary *currencyDictionary = [[NSMutableDictionary alloc] init];
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
     NSString *path=[paths objectAtIndex:0];
     NSString *fileName=[path stringByAppendingPathComponent:@"currency.plist"];
     [currencyDictionary writeToFile:fileName  atomically:YES];

在UIView創造一個Animation:

     [UIView beginAnimations:@"View Flip" context:nil];
     [UIView setAnimationDuration:1.25];
     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
     [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view.superview cache:YES];
     [self.view removeFromSuperview];
     [UIView commitAnimations]

導入GPS定位:
記得要先#import <CoreLocation/CoreLocation.h>。

 - (void)setLocationManager{
     CLLocationManager *clLocationManager = [[CLLocationManager alloc] init];
     [clLocationManager setDesiredAccuracy:kCLLocationAccuracyBest];
     [clLocationManager setDelegate:self]; // deldegate
     [clLocationManager startUpdatingLocation];
     [clLocationManager release];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
     [manager stopUpdatingLocation]; // 停止执行此方法
     newLocation.coordinate.longitude; // 获取当前的经度
     newLocation.coordinate.latitude; // 获取当前的纬度
}

得到UUID的值:

     CFUUIDRef uuidObj = CFUUIDCreate(nil);
     NSString *uuidString = (NSString *)CFUUIDCreateString(nil, uuidObj);
     CFRelease(uuidObj); // uuidString就是唯一得了
     [uuidString release];

讓Icon上不加光暈弧線:
在****-info.plist中增加一個項目:Icon already includes gloss effects,然後勾選value的選框即可。

獲得現在時間:

[NSString stringWithFormat:@"%d", time(NULL)];

相機鏡頭前後調換:

#import 
 
// Switching between front and back cameras
 
- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position
{
    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for ( AVCaptureDevice *device in devices )
        if ( device.position == position )
            return device;
    return nil;
}
 
- (void)swapFrontAndBackCameras {
    // Assume the session is already running
 
    NSArray *inputs = self.session.inputs;
    for ( AVCaptureDeviceInput *input in inputs ) {
        AVCaptureDevice *device = input.device;
        if ( [device hasMediaType:AVMediaTypeVideo] ) {
            AVCaptureDevicePosition position = device.position;
            AVCaptureDevice *newCamera = nil;
            AVCaptureDeviceInput *newInput = nil;
 
            if (position == AVCaptureDevicePositionFront)
                newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack];
            else
                newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront];
            newInput = [AVCaptureDeviceInput deviceInputWithDevice:newCamera error:nil];
 
            // beginConfiguration ensures that pending changes are not applied immediately
            [self.session beginConfiguration];
 
            [self.session removeInput:input];
            [self.session addInput:newInput];
 
            // Changes take effect once the outermost commitConfiguration is invoked.
            [self.session commitConfiguration];
            break;
        }
    }
}


將UIView作成圓角:

    m_view1.layer.cornerRadius = 10; // 设置边框得圆弧度
    m_view1.layer.masksToBounds = YES;
 
    m_view2.layer.cornerRadius = 10;
    m_view2.layer.masksToBounds = YES;
    m_view2.layer.borderWidth = 15; // 设置这个圆形边框得宽度


在UIImage上添加文字

-(UIImage *)addText:(UIImage *)img text:(NSString *)someText{ 
    int w = img.size.width;
    int h = img.size.height; 
    //lon = h - lon;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
    CGContextSetRGBFillColor(context, 255, 255, 255, 1);
    UIGraphicsPushContext(context);
    CGContextScaleCTM(context, 1, -1);
    CGContextTranslateCTM(context, 0.0, -40);
    CGContextRotateCTM(context, 0.0f);
    [someText drawAtPoint:CGPointMake(15, 10) withFont: [UIFont fontWithName:@"Arial" size:14]];
    UIGraphicsPopContext();
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    return [UIImage imageWithCGImage:imageMasked];
}

沒有留言:

張貼留言

歡迎大家留言提問,我會答的都會盡力回答!
如果太久沒出現回應就是我又忘記回來看留言了TAT