as the title i have problem with the picker controller. i'm using a virtual machine to program in objective c, xcode version 9.2, and iOS 11.2. My app is an Address Book, when the user create a new contact he is able to pick a photo from one camera (if run on a device) o select a photo from photo roll. i manage this feature with the following code:
-(void) imagePickerController:(UIImagePickerController *)_picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if (CFStringCompare((CFStringRef) mediaType, kUTTypeImage, 0) ==
kCFCompareEqualTo) {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
if(image==nil)
image = [info objectForKey:UIImagePickerControllerEditedImage];
if(image==nil)
image = [info objectForKey:UIImagePickerControllerCropRect];
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
PHAsset *asset = nil;
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
if (fetchResult != nil && fetchResult.count > 0) {
// ottengo l'ultima foto
asset = [fetchResult lastObject];
}
if (asset) {
// ottengo le info della foto tramite l'asset
PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];
imageRequestOptions.synchronous = YES;
[[PHImageManager defaultManager]
requestImageDataForAsset:asset
options:imageRequestOptions
resultHandler:^(NSData *imageData, NSString *dataUTI,
UIImageOrientation orientation,
NSDictionary *info)
{
NSLog(@"info = %@", info);
if ([info objectForKey:@"PHImageFileURLKey"]) {
imagepath = [NSString stringWithFormat:@"%@", [info objectForKey:@"PHImageFileURLKey"]];
}
}];
}
}
else {
NSURL *url = [[NSURL alloc] init];
url = [info valueForKey:UIImagePickerControllerImageURL];
imagepath = [url absoluteString];
}
}
[picker dismissViewControllerAnimated:YES completion:NULL]; }
in the first part i check that the user select a photo instead a video, then i save image in an uiimage variable (correctly in both case), at last i check sorcetype of image picker controller and if it is set to sourcetype camera i save photo in photo roll and i obtain with asset last photo path from photo roll (if you know easier way i appreciate), else i save image path as NSStringwith absoluteString of url.
i obtain in both methods a path like: file:///Users/dannycasa/Library/Developer/CoreSimulator/Devices/F9730203-BFD1-4C22-BC52-DC2FD6E736C3/data/Containers/Data/Application/57839B62-0573-49D7-896A-3606007F264B/tmp/34AE1FDA-0B1F-4310-8C7A-CD91CF46104D.jpeg
the problem is that when i try to show image in another view controller with:
NSData *data = [NSData dataWithContentsOfFile:self.contact.profileImageUrl];
image = [[UIImage alloc] initWithData:data];
or
NSURL *url = [[NSURL alloc] initWithString:self.contact.profileImageUrl];
NSData *data = [NSData dataWithContentsOfUrl:url];
image = [[UIImage alloc] initWithData:data];
image is always null! In both view controller (new contact and show contact view controller), i import Photos/Photos.h and MobileCoreServices/MobileCoreServices.h. Any suggestions?