尝试从相机捕获图像时出错
Getting Error When Trying To Capture Image From Camera
这是代码。它有 takePhoto 加载相机的方法。 selectPhoto for CameraRoll。
当我尝试从相机胶卷中 select 照片时效果很好,我可以上传图像。
当我尝试从相机拍摄照片并上传时,应用程序崩溃了。
- (void)takePhoto {
if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *deviceNotFoundAlert = [[UIAlertView alloc] initWithTitle:@"No Device" message:@"Camera is not available"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[deviceNotFoundAlert show];
}else{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}
- (void)selectPhoto {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
//picker.modalPresentationStyle = UIModalPresentationCurrentContext;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
chosenImage = info[UIImagePickerControllerEditedImage];
self.imgUser.image = chosenImage;
NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *representation = [myasset defaultRepresentation];
selectedFileName = [representation filename];
NSLog(@"fileName : %@",selectedFileName);
encodedImage = [UIImageJPEGRepresentation(chosenImage, 0.8) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//NSLog(@"Encoded Image : %@", encodedImage);
NSString *newImage;
NSString *newImage1;
newImage = [encodedImage stringByReplacingOccurrencesOfString:@"+"
withString:@"%2B"];
newImage = [newImage stringByReplacingOccurrencesOfString:@"/"
withString:@"%2F"];
newImage1 = [newImage stringByReplacingOccurrencesOfString:@"="
withString:@"%3D"];
//NSLog(@"Encoded Image : %@", newImage1);
NSLog(@"Selected File Name : %@", selectedFileName);
//Sending Upload Request
[activityIndicator startAnimating];
[[MyService MySingleton] sendRequestWithHeader:_headerString param:[NSArray arrayWithObjects:newImage1,selectedFileName, nil] msg:@"uploadPhoto"];
};
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:imageURL
resultBlock:resultblock
failureBlock:nil];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
得到以下东西:
对尚未呈现的视图进行快照会生成空快照。确保您的视图在屏幕更新之前或快照之前至少渲染过一次。
拍照时也将图像名称设为 null 并尝试在 didFinishPickingMediaWithInfo 方法中访问它。
我已经解决了图像为空的问题。使用以下代码:
if (representation.filename!=nil) {
selectedFileName = [representation filename];
}else{
selectedFileName = @"user.jpeg";
}
这是代码。它有 takePhoto 加载相机的方法。 selectPhoto for CameraRoll。
当我尝试从相机胶卷中 select 照片时效果很好,我可以上传图像。
当我尝试从相机拍摄照片并上传时,应用程序崩溃了。
- (void)takePhoto {
if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *deviceNotFoundAlert = [[UIAlertView alloc] initWithTitle:@"No Device" message:@"Camera is not available"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[deviceNotFoundAlert show];
}else{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}
- (void)selectPhoto {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
//picker.modalPresentationStyle = UIModalPresentationCurrentContext;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
chosenImage = info[UIImagePickerControllerEditedImage];
self.imgUser.image = chosenImage;
NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *representation = [myasset defaultRepresentation];
selectedFileName = [representation filename];
NSLog(@"fileName : %@",selectedFileName);
encodedImage = [UIImageJPEGRepresentation(chosenImage, 0.8) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//NSLog(@"Encoded Image : %@", encodedImage);
NSString *newImage;
NSString *newImage1;
newImage = [encodedImage stringByReplacingOccurrencesOfString:@"+"
withString:@"%2B"];
newImage = [newImage stringByReplacingOccurrencesOfString:@"/"
withString:@"%2F"];
newImage1 = [newImage stringByReplacingOccurrencesOfString:@"="
withString:@"%3D"];
//NSLog(@"Encoded Image : %@", newImage1);
NSLog(@"Selected File Name : %@", selectedFileName);
//Sending Upload Request
[activityIndicator startAnimating];
[[MyService MySingleton] sendRequestWithHeader:_headerString param:[NSArray arrayWithObjects:newImage1,selectedFileName, nil] msg:@"uploadPhoto"];
};
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:imageURL
resultBlock:resultblock
failureBlock:nil];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
得到以下东西:
对尚未呈现的视图进行快照会生成空快照。确保您的视图在屏幕更新之前或快照之前至少渲染过一次。
拍照时也将图像名称设为 null 并尝试在 didFinishPickingMediaWithInfo 方法中访问它。
我已经解决了图像为空的问题。使用以下代码:
if (representation.filename!=nil) {
selectedFileName = [representation filename];
}else{
selectedFileName = @"user.jpeg";
}