Category Archives: 허접프로그래머

[iPhone] URL주소를 가지고 원격지의 이미지 파일을 UIImage로 읽어오기

UIImage를 이용하여 로컬 리소스 파일로 존재하는 이미지 파일을 읽어오는 것은 많이 해보셨을 것입니다.

이번에는 원격지의 이미지를 읽어오도록 하는 것을 시도해 보겠습니다. 다음의 소스를 참고하세요.

[code]- (void)applicationDidFinishLaunching:(UIApplication *)application {
   
    // 이미지를 읽어올 주소
    NSURL *url = [NSURL URLWithString:@”http://www.segye.com/content/image/2009/01/06/20090106000247_0.jpg”];
    NSData *data = [NSData dataWithContentsOfURL:url];
   
    // 데이터가 정상적으로 읽혔는지 확인한다. 네트워크가 연결되지 않았다면 nil이다.
    if(data) {
        UIImage *image = [[UIImage alloc] initWithData:data];
        UIImageView *view = [[UIImageView alloc] initWithImage:image];
   
        [view setFrame:CGRectMake(0.0f, 0.0f, image.size.width, image.size.height)];
        [window addSubview:view];
        [view release];
        [image release];
    }
    // 데이터를 정상적으로 읽어오지 못했을 경우의 처리
    else
    {
        UILabel *label = [[UILabel alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
       
        [label setText:@”Fail…”];
        [label setTextAlignment:UITextAlignmentCenter];
        [window addSubview:label];
        [label release];
    }
   
    [window makeKeyAndVisible];
}[/code]
이 코드에서 중요하게 보아야 할점은 NSData가 nil인지 아닌지를 판단하여 데이터를 가져왔는지 여부를 확인할 수 있다는 점입니다.

또한 가져온 이미지를 UIImage로 읽어들인 후에 size구조체에 접근하여 이미지의 크기를 얻어올 수 있습니다.
사용자 삽입 이미지어이쿠, 꽃보다 남자의 이미지가 아이팟의 가로길이인 320보다 커서 좀 짤렸네요;; 미안하다 잔디야~ㅠㅠ

이미지는 Frame의 가로 세로 길이에 맞춰 늘어나게 됩니다.
1002669745.zip

[iPhone] 아이폰의 Unique Identifier 알아내기

아이폰의 장치에는 일명 UUID라는 이름의 식별자를 가지고 있습니다. 대략 숫자와 영문이 섞인 40자리 문자열입니다.

이를 가져와서 뿌리는 방법은 다음과 같습니다.

[code]- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    NSString* strId = [[UIDevice currentDevice] uniqueIdentifier];
    UILabel *label = [[UILabel alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    
    [label setFont:[UIFont systemFontOfSize:12.0f]];
    [label setTextAlignment:UITextAlignmentCenter];
    [label setText:strId];
    [window addSubview:label];
    [label release];
    
    [window makeKeyAndVisible];
}[/code]

위와 같이 간단하게 한줄로 UUID 값을 얻어올 수 있습니다. 너무 쉬웠나요? ^^a
사용자 삽입 이미지이런 형식의 데이터를 가져올 수 있습니다. 다만 위의 값은 시뮬레이터의 값이고 실제 값은 -없이 문자로 이루어져 있습니다.