objective-c

[objective-c]UIWebViewのhtmlにUIImageをimgタグで表示

UIWebViewのhtmlにUIImageをimgタグで表示

NSData *imageData = UIImagePNGRepresentation(image);
NSString* base64 = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
base64 = [base64 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString*js = [NSString stringWithFormat:@"var img=document.createElement('img');img.src='data:image/jpg;base64,%@';document.body.appendChild(img);",base64];
[mainWebView stringByEvaluatingJavaScriptFromString:js];

[objective-c]UIWebViewでリンククリックで処理

UIWebViewでリンククリックで処理

読み込むhtml

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<a href="js-call://camera">camera</a><br/><br/>
<a href="js-call://album">album</a><br/><br/>
<a href="js-call://alert">alert</a><br/><br/>
</body>
</html>

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"shouldStartLoadWithRequest");
    NSString *requestString = [[request URL] absoluteString];
    NSString *protocol = @"js-call://";
    if ([requestString hasPrefix:protocol]) {
        NSString *requestContent = [requestString substringFromIndex:[protocol length]];
        NSArray *vals = [requestContent componentsSeparatedByString:@"/"];
        if ([vals[0] isEqualToString:@"camera"]){
            [self startCamera];//カメラ起動
        }else if ([vals[0] isEqualToString:@"album"]){
            [self startAlbum];//カメラロール起動
        }else {
            [webView stringByEvaluatingJavaScriptFromString:@"alert('Not defined');"];//jsのalert実行
        }
        return NO;
    }
    return YES;
}

[objective-c]UIViewをUIImageへ(Retina対応)

UIViewをUIImageへ(Retina対応)

-(UIImage*)getUIImageByUIView:(UIView*)view
{
    UIImage *screenImage;
    //UIGraphicsBeginImageContext(size);
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    screenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenImage;
}

[objective-c]UIColorから16進数の文字列を得る

UIColorから16進数の文字列を得る

NSString*colorHex = [self getColorHexByUIColor:[UIColor redColor]];
NSLog("%@",colorHex);

-(NSString*)getColorHexByUIColor:(UIColor*)color
{
    CGFloat r,g,b,a;
    [color getRed:&r green:&g blue:&b alpha:&a];
    return [NSString stringWithFormat:@"%02x%02x%02x",(int)(r*255.0),(int)(g*255.0),(int)(b*255.0)];
}

[objective-c]UILabelを複製する

UILabelを複製する

-(UILabel*)getCloneUILabelByUILabel:(UILabel*)label
{
    NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:label];
    return [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
}
UILabel*cloneLabel = [self getCloneUILabelByUILabel:modelLabel];

[objective-c]2つの日付の差を日数で出す

2つの日付の差を日数で出す

int dayCount = [self getDaysCountByTwoDateString:@"2014-04-01" endDateString:@"2014-04-04"];
-(int)getDaysCountByTwoDateString:(NSString*)startDateString endDateString:(NSString*)endDateString
{
    NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
    [inputDateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *startInputDate = [inputDateFormatter dateFromString:startDateString];
    NSDate *endInputDate = [inputDateFormatter dateFromString:endDateString];
    
    float tmp= [endInputDate timeIntervalSinceDate:startInputDate];
    int day=(int)( tmp / (3600.0*24.0) );
    return day;
}

[objective-c]animateWithDurationを途中で止める

animateWithDurationを途中で止める

アニメーション実行

[UIView animateWithDuration:10.0f animations:^{
    targetView.frame=CGRectMalke(0.0,0.0,100.0,100.0);
} completion:^(BOOL finished) {
    
}];

アニメーション中止

[UIView setAnimationBeginsFromCurrentState:YES];
[UIView animateWithDuration:0.001 animations:^{
    targetView.alpha = 1.0;
}];