IanMK2 Blog

HTTP의 POST를 이용한 이미지 전송이다. 간단간단해서 좋다


//XCODE상의 소스
+ (IBAction)uploadImage:(UIImage *)image:(NSString*)filename {
NSData *imageData = UIImageJPEGRepresentation(image, 90);
NSString *urlString = @"이미지전송을 위한 PHP경로";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];  // important!!!
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
}

//PHP 코드

<? 
$uploaddir = './uploads/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "SUCCESS";
}
?>
Posted by IanMK2
 공백과 탭 제거

nowStr = [nowStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];


 공백과 탭 및 개행제거

nowStr = [nowStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];



[문자열 검색과 비교 함수]

- ( BOOL ) hasPrefix: (NSString *) string; //인자가 리시버 앞부분과일치하는지

- ( BOOL ) hasSuffix: (NSString *) string; //뒷부분

- ( NSRange ) rangeOfString: (NSString *)string; //부분문자열검색

- ( NSRange ) rangeOfString: (NSString *)string options:(NSStringCompareOptions)mask

- ( NSArray ) componentSeperateByString: (NSString *)string; //인자를토큰으로하여분리한문자열배열을만든다. split 함수를 의미

 - ( BOOL ) isEqualToString: (NSString *)string; //문자열이 동일한지 비교

- ( NSComparisonResult) compare: (NSString *) string;//문자열이 동일한지 비교

- ( NSComparisonResult) compare: (NSString *) string option: (unsigned) mask;

//문자열이 동일한지 비교 옵션추가하여 비교


옵션

[mask]

NSCaseInsensitiveSearch : 대소문자를 무시하고  문자열을 비교한다.

NSLiteralSearch : 대소문자를 구분하여  문자열을 비교한다.

NSNumericSearch : 숫자문자를 숫자  자체로 인식( Filename9.txt < Filename20.txt < Filename100.txt )  

NSBackwardsSearch : 문자열의 뒤에서 부터 문자열을 비교한다.

NSAnchoredSearch : 문자열의 시작(NSBackwardsSearch 지정시 부분과 비교대상이 일치하는지 비교한다.(hasPrefix 같음)



사용예)

대소문자 구별없이 검색 (검색어가 문자열의 위치와는 상관 없음 즉 가운데 검색어가 있어도 검색)

NSRange rSearchCity = [원본문자열 rangeOfString:검색어 options:NSCaseInsensitiveSearch];


대소문자를 구별하지 않고 문자열 앞부터 일치하는지 검색

NSRange rSearchCity = [원본문자열 rangeOfString:검색어 

                                      options:(NSCaseInsensitiveSearch | NSAnchoredSearch)]



출처 - 단비스튜디오

Posted by IanMK2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"MatzipCustomCell";
    
    MatzipCustomCell *cell = (MatzipCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray * nib= [[NSBundle mainBundle] loadNibNamed:@"MatzipCustomCell" owner:self options:nil];
cell = (MatzipCustomCell*)[nib objectAtIndex:0];        
UIView *bgv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, cell.frame.size.width)];
[bgv setBackgroundColor:[UIColor colorWithRed:1.0f green:50.0f/255.0f blue:50.0f/255.0f alpha:1.0f]];
[cell setSelectedBackgroundView:bgv];
[bgv release];
    }
NSMutableDictionary *ary = [XMLData objectAtIndex:indexPath.row];
cell.nameLabel.text = [ary objectForKey:@"name"];
cell.numLabel.text = [NSString stringWithFormat:@"%@명 평가", [ary objectForKey:@"reviewCnt"] ];
[cell.imageView setImage:[UIImage imageNamed:@"star.png"]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
임시적으로 뷰를 생성하고 색을넣은다음 그것을 셀의 선택색으로 지정한다.
Posted by IanMK2
현재 위치와 어느한지점을 지도에 뿌리기위해 맵뷰를 쓸일이 생겼다.

사전작업은 다음과 같다.
프레임워크에서 CoreLocation.framework를 추가
헤더는 MapKit/MapKit.h를 추가
일단 원하는 위치에 핀을 박기위한 클래스
///////////////////PlaceMark.h//////////////////////////////////////////////
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface PlaceMark : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, assign) NSString *title;
@property (nonatomic, assign) NSString *subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;

@end


//////////////////PlaceMark.m/////////////////////////////////////////////
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface PlaceMark : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, assign) NSString *title;
@property (nonatomic, assign) NSString *subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;

@end



실제 사용소스시
        MKMapView *gmap = [[MKMapView alloc] initWithFrame:self.view.bounds];
gmap.showsUserLocation = TRUE; //자기위치 표시
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.005;//표시되는 영역의 설정 수치가 작을수록 더 확대된다.
span.longitudeDelta= 0.005;
CLLocationCoordinate2D location = gmap.userLocation.coordinate;
location.latitude=[[storedata objectForKey:@"latitude"] floatValue];//GPS좌표경위도설정
location.longitude=[[storedata objectForKey:@"longitude"] floatValue];
region.span = span;
region.center = location;
[gmap setRegion:region animated:TRUE];
[gmap regionThatFits:region];
PlaceMark *placemark = [[PlaceMark alloc] initWithCoordinate:location]; //핀박을위치설정
placemark.title = @"Title"
placemark.subtitle = @"SubTitle";

[gmap addAnnotation:placemark];  //핀을 박아넣는다.
[self.view addSubview:gmap]; //뷰에 추가

[placemark release];
[gmap release];


Posted by IanMK2
-(void)textViewDidBeginEditing:(UITextView *)textView{
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"완료"  style:UIBarButtonSystemItemSave target:self action:@selector(done)];

[self.navigationItem setRightBarButtonItem:doneButton];
[doneButton release];
[scrollview setContentOffset:CGPointMake(0, 190) animated:YES];
}
-(void)done{
[self.navigationItem setRightBarButtonItem:nil];
[self.textview resignFirstResponder];
[scrollview setContentOffset:CGPointMake(0, 0) animated:YES];
}

텍스트뷰에 멀티라인을 입력한 후 편집모드를 나오기 위해 네비게이션에 완료버튼을 추가하고
버튼을 누르면 키보드를 집어넣는 기능이다. 덤으로 스크롤뷰도 스크롤한다.
Posted by IanMK2