App Develop

[Swift]クラス拡張

クラス拡張 objective-cにおけるカテゴリ

extension UIView {
    var left:CGFloat{
        get{
            return self.frame.origin.x
        }
        set{
            self.frame = CGRectMake(newValue, self.frame.origin.y, self.frame.size.width, self.frame.size.height)
        }
    }
    func toUIImage() -> UIImage{
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0.0)
        self.layer.renderInContext(UIGraphicsGetCurrentContext())
        let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

[objective-c]NSTimerがFireしない

NSTimerがFireしない
UIAlertViewのYes,Noボタンをタップしたタイミングと重なったり、
何らかの要因によりNSTimerがサブスレッドで実行されることが原因

対処法

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(methodName) userInfo:nil repeats:NO];

これを

dispatch_async(dispatch_get_main_queue(), ^{
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(methodName) userInfo:nil repeats:NO];
});

こうする

[objective-c]日付文字列まとめ

日付文字列まとめ

_currentDate =  [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone systemTimeZone] secondsFromGMT]];

NSDateFormatter* form = [[NSDateFormatter alloc] init];
[form setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[form setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
_currentDatetimeString = [form stringFromDate: _currentDate ];
[form setDateFormat:@"yyyy-MM-dd"];
_currentDateString = [form stringFromDate: _currentDate ];
[form setDateFormat:@"yyyy-MM"];
_currentYMString = [form stringFromDate: _currentDate ];
[form setDateFormat:@"HH:mm:ss"];
_currentTimeString = [form stringFromDate: _currentDate ];

[objective-c]RecurrenceなEKEventを全て削除

RecurrenceなEKEventを全て削除

EKEventStore*eventstore = [[EKEventStore alloc] init];
EKEvent*firstEvent = [eventstore eventWithIdentifier:targetRecurrenceEvent.eventIdentifier];
NSError*error;
BOOL result = [eventstore removeEvent:firstEvent span:EKSpanFutureEvents commit:YES error:&error];

[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];