2011-07-23 5 views
1

UIImagePickerControllerはフリーズしていますが、イメージが圧縮されているか、NSURLConnectionによって送信されていると思いますが、後でそれがわかっていません。UIIMagePickerController&NSURLConnection

私が望むのは、すべてこれがバックグラウンドで行われることです。代わりに、私は図書館から写真を選ぶと、画面は永遠のような気がするようにフリーズします。明らかに最適ではありません。私はここでどのようなアプローチを取るべきですか?

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo 
{ 
int i = 0; 
NSString *uniquePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/selectedImage.png"]; 

while ([[NSFileManager defaultManager] fileExistsAtPath:uniquePath]) 
{ 
    uniquePath = [NSString stringWithFormat:@"%@/%@-%d.%@", [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"], @"selectedImage", ++i, @"png"]; 
} 

NSLog(@"Writing selected Image to Documents Folder, %@", uniquePath); 

dataForPNGFile = UIImagePNGRepresentation(img); 

if (!dataForPNGFile) return NO; 

[UIImagePNGRepresentation(img) writeToFile:uniquePath atomically:YES]; 
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); 
[[self parentViewController] dismissModalViewControllerAnimated:YES]; 
[picker release]; 

NSString *urlString = @"http://localhost:3000/photos"; 
NSURL *url = [NSURL URLWithString:urlString]; 
NSString *boundary = @"----1010101010"; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

NSString *photoPath = [[NSBundle mainBundle] pathForResource:@" my-photo " ofType:@"jpg"]; 
NSData *photoData = [NSData dataWithContentsOfFile:photoPath]; 

NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Disposition: form-data; name=\"photo-description\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"testing 123" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
// [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo-file\"; filename=\"%@\"\r\n", " my-photo.jpg"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:photoData]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Disposition: form-data; name=\"tags\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"random,test,example" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setHTTPBody:body]; 
[request addValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField: @"Content-Length"]; 

[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

}

答えて

0

使用すべき:

detachNewThreadSelector:toTarget:withObject:

新しいスレッドを切り離し、スレッドエントリポイントとして指定されたセレクタを使用します。

+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument 

ここドキュメント:NSThread

あなたのUIは唯一のように、新しいスレッド用のNSAutoreleasePoolを作成するために覚えて影響されませんので、これが、CPUを食べている操作のための新しいスレッドを作成します。メインスレッドはデフォルトでNSAutoreleasePoolプールを取得します。あなたのセレクタの冒頭にそう

は行います

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

//and at the end: 

[pool drain]: