0

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?

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Danny CASA
  • 21
  • 6
  • The file URL is temporary. The contents can be deleted in any time, once the picker is deallocated. You will have to copy the file to the app filesystem. – Sulthan Mar 10 '18 at 18:10
  • i save variable imagepath of the first block of code in a object, then i pass that object with segue – Danny CASA Mar 10 '18 at 18:38
  • You are saving a path to a temporary file that will no longer exist after your controller is dismissed. Your `NSData` object is probably `nil` and no image gets created. If your aim is to get the image, why don't you return the image (`UIImage`) directly from the picker? Instead of just the URL? – Sulthan Mar 10 '18 at 18:59
  • xcode doesn't permit me to use uiimage as argument for initializer of object.. – Danny CASA Mar 10 '18 at 19:32
  • imagepath is a string variable declared after the @implementation within {}, so it exists over the picker controller. – Danny CASA Mar 10 '18 at 19:41

0 Answers0