Here is the code. It has takePhoto method for loading camera. selectPhoto for CameraRoll.
When I am trying to select photo from camera roll it works well and i can upload the image.
While I am trying to take photo from camera and upload than the app crashes.
- (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];
}
Getting Following thing :
Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
Also getting image name as null when taking a picture and trying to access it in didFinishPickingMediaWithInfo method.
I have solved the issue as image was coming as null. With the following code :
if (representation.filename!=nil) {
selectedFileName = [representation filename];
}else{
selectedFileName = @"user.jpeg";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.