誰かお願いします... #$%^ これを見てください。jpeg 表現で setData を使用して、デバッガーをウォークスルーする日。ios4 アセット ライブラリを使用してファイルを設定し、新しい PHP スクリプトを試し、asiHTTPrequest ファイルを削除し、新しいファイルがあることを確認します。まだ何も...コードの半分は、ここまたは Web 上の他の場所の例からまとめられています。
ここでの目標は、カメラロールから写真を選択してアップロードすることです。かなり簡単に思えます。デスクトップから正常に動作する別の PHP スクリプトがあり、ここから 1 つを取得しました。これははるかに簡潔で、デスクトップも。
画像のピッキングを終了するためのオーバーライド
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// dealing with a still image
if(CFStringCompare((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo){
editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
originalImage = (UIImage*) [info objectForKey:UIImagePickerControllerOriginalImage];
/*
if(editedImage){
imageToSave = editedImage;
} else {
imageToSave = originalImage;
}
*/
chosenImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
//_imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation(originalImage, 0.0)];
//_imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation(chosenImage.image, 0.0)];
UIImage *im = [info objectForKey:@"UIImagePickerControllerOriginalImage"] ;
UIGraphicsBeginImageContext(CGSizeMake(320,480));
[im drawInRect:CGRectMake(0, 0,320,480)];
_resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation(_resizedImage, 0.0)];
}
[picker release];
}
次にアップロード方法。
-(void)uploadPhoto
{
//NSLog(@"image path inside uploadPhoto --> %@", _imagePath);
NSLog(@"uploadPhoto");
//NSLog(@"%@", imageData);
//_imageData = _imageData;
NSString *unescapedURL = @"http://dev.xxxx.com/upload.php";
NSString * escapedURL =
(NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)unescapedURL,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8 );
NSURL *url = [NSURL URLWithString:unescapedURL];
//NSURL *url = [NSURL URLWithString:unescapedURL];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request setRequestMethod:@"POST"];
//[request setStringEncoding:NSUTF8StringEncoding];
//[request addPostValue:@"submit" forKey:@"Submit"];
//[request setPostValue:@"Submit" forKey:@"Submit"];
[request setData:_imageData withFileName:@"image4.jpg" andContentType:@"image/jpeg" forKey:@"photo"];
//[request setFile:_imagePath forKey:@"photo"];
//[request setFile:_imagePath withFileName:@"image5.png" andContentType:@"image/png" forKey:@"photo"];
[request setDidFailSelector:@selector(requestFailed:)];
[request setDidFinishSelector:@selector(requestFinished:)];
[request setTimeOutSeconds:500];
[request startAsynchronous];
NSError *error = nil;
NSString *theString = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
if( theString )
{
NSLog(@"Text=%@", theString);
}
else
{
NSLog(@"Error = %@", error);
NSString *localized = [error localizedDescription];
NSString *localizedFail = [error localizedFailureReason] ? [error localizedFailureReason] : NSLocalizedString(@"not it", nil);
NSLog(@"localized error--> %@", localized);
NSLog(@"localizedFail--> %@", localizedFail);
}
[escapedURL release];
}
次に、終了/失敗セレクター
-(void)requestFinished:(ASIFormDataRequest *)request
{
NSLog(@"requestFinished");
NSString *respondingString = [request responseString];
NSLog(@"response string--> %@", respondingString);
NSData *responseData = [request responseData];
NSLog(@"%@", responseData);
}
-(void)requestFailed:(ASIFormDataRequest *)request
{
NSLog(@"requestFailed");
//NSError *error = [request error];
//NSLog(@"%@", [error description]);
}
ヘルプ!溺死...