2014년 1월 21일 화요일

sns 공유 (Twitter, Facebook, Line)

+ (void)postToTwitter:(UIViewController *)originController  withData:(NSDictionary *)dic {
    SLComposeViewController *tweetSheet = [SLComposeViewController
                                           composeViewControllerForServiceType:
                                           SLServiceTypeTwitter];
    tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
        switch(result) {
                //  This means the user cancelled without sending the Tweet
            case SLComposeViewControllerResultCancelled:
                break;
                //  This means the user hit 'Send'
            case SLComposeViewControllerResultDone:
                break;
        }
    };
    
    //  Set the initial body of the Tweet
    [tweetSheet setInitialText:[dic valueForKey:@"text"]];
    NSString *imageUrl = [dic valueForKey:@"imageurl"];
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if(![tweetSheet addImage:[UIImage imageWithData:data]]) {
            NSLog(@"Unable to add the image!");
        } else {
            if (![tweetSheet addURL:[NSURL URLWithString:[dic valueForKey:@"referenceurl"]]]){
                NSLog(@"Unable to add the URL!");
            }
            [originController presentViewController:tweetSheet animated:YES completion:^{
                NSLog(@"Tweet sheet has been presented.");
            }];
        }
    }];
}

+ (void)postToFacebook:(UIViewController *)originController withData:(NSDictionary *)dic {
    SLComposeViewController *facebookSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    
    [facebookSheet setInitialText:[dic valueForKey:@"text"]];
    [facebookSheet addURL:[NSURL URLWithString:[dic valueForKey:@"referenceurl"]]];
    NSString *imageUrl = [dic valueForKey:@"imageurl"];
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        [facebookSheet addImage:[UIImage imageWithData:data]];
    }];
    
    [originController presentViewController:facebookSheet animated:YES completion:Nil];
}

+ (void)postToLine:(UIViewController *)originController withData:(NSDictionary *)dic {
    //텍스트 전송 링크 전송
    NSString *_resultString = [[NSString stringWithFormat:@"%@%@",[dic valueForKey:@"text"],[dic valueForKey:@"imageurl"]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *LineUrlString = [NSString stringWithFormat:@"line://msg/text/%@",_resultString];
    BOOL r =[[UIApplication sharedApplication] openURL:[NSURL URLWithString:LineUrlString]];
    if (!r) {
        [[[UIAlertView alloc] initWithTitle:@"" message:@"LINEがインストールされていません。" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil,nil] show];
    }

}

이미지 리사이징(이율 유지)

//이미지 비율을 유지하면서 최대 가로 or 높이보다 작은 이미지 사이즈 생성
+ (UIImage *)getScaledImage:(UIImage *)image scaledToMaxWidth:(CGFloat)width maxHeight:(CGFloat)height {
    CGFloat oldWidth = image.size.width;
    CGFloat oldHeight = image.size.height;
    
    if (oldWidth < width && oldHeight < height)
        return image;
    
    CGFloat scaleFactorW =1;
    CGFloat scaleFactorH =1;
    
    if (oldWidth > width)
        scaleFactorW = width / oldWidth;
    if(oldHeight > height)
        scaleFactorH = height / oldHeight;
    
    CGFloat scaleFactor = (scaleFactorW<scaleFactorH)?scaleFactorW:scaleFactorH;
    
    
    CGFloat newHeight = oldHeight * scaleFactor;
    //CGFloat newWidth = oldWidth * scaleFactor;
    CGSize newSize = CGSizeMake(width, newHeight);
    
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

//이미지 비율을 유지하면서 최대 가로 보다 작은 이미지 사이즈 생성
+ (UIImage *)getScaledImage:(UIImage *)image scaledToMaxWidth:(CGFloat)width {
    CGFloat oldWidth = image.size.width;
    CGFloat oldHeight = image.size.height;
    
    CGFloat scaleFactor=1;
    
    if (oldWidth > width) {
        scaleFactor = width / oldWidth;
    } else  //oldWidth<width and height==0이면, scale하지 않음.
        return image;
    
    CGFloat newHeight = oldHeight * scaleFactor;
    CGFloat newWidth = oldWidth * scaleFactor;
    CGSize newSize = CGSizeMake(newWidth, newHeight);
    
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;

}

2014년 1월 15일 수요일

이미지 패턴으로 컬러값 지정

[UIColor colorWithPatternImage:[UIImage imageNamed:@"이미지 이름"];


example)
//배경을 bh_sand.png 파일로 설정 하기
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_sand"]];