Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

[iOS 18.0] PHImageManager request image crash

I am seeing a crash on iOS 18.0 in my app due to PHImageManager.default().requestImage. Same crash is seen even while using requestImageDataAndOrientation. Not sure why this is happening only on iOS 18.0.

Any help on this would be appreciated.

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000010
Exception Codes: 0x0000000000000001, 0x0000000000000010
VM Region Info: 0x10 is not in any region.  Bytes before following region: 4310777840
      REGION TYPE                 START - END      [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
Termination Reason: SIGNAL 11 Segmentation fault: 11
Terminating Process: exc handler [695]

Triggered by Thread:  14
Thread 14 name:   Dispatch queue: */file=33) .Generic
Thread 14 Crashed:
0   libobjc.A.dylib               	       0x1944d7020 objc_msgSend + 32
1   PhotoLibraryServices          	       0x1b080262c +[PLUniformTypeIdentifier utiWithCompactRepresentation:conformanceHint:] + 40
2   Photos                        	       0x1b0081354 _resourceInfoFromResultDict + 1048
3   Photos                        	       0x1b0080a7c fetchResourcesForChoosing + 584
4   Photos                        	       0x1b0080714 ___fetchNonHintResources_block_invoke.227 + 152
5   PhotoLibraryServices          	       0x1b026b3c4 __53-[PLManagedObjectContext _directPerformBlockAndWait:]_block_invoke + 48
6   CoreData                      	       0x19f171b00 developerSubmittedBlockToNSManagedObjectContextPerform + 228
7   libdispatch.dylib             	       0x19eeff584 _dispatch_client_callout + 16
8   libdispatch.dylib             	       0x19eef5728 _dispatch_lane_barrier_sync_invoke_and_complete + 56
9   CoreData                      	       0x19f1c2a0c -[NSManagedObjectContext performBlockAndWait:] + 308
10  PhotoLibraryServices          	       0x1b026cea4 -[PLManagedObjectContext _directPerformBlockAndWait:] + 144
11  PhotoLibraryServices          	       0x1b026cdf8 -[PLManagedObjectContext performBlockAndWait:] + 196
12  Photos                        	       0x1b00804a4 _fetchNonHintResources + 292
13  Photos                        	       0x1afeedb38 PHChooserListContinueEnumerating + 144
14  Photos                        	       0x1afeed9f8 -[PHImageResourceChooser presentNextQualifyingResource] + 412
15  Photos                        	       0x1afeed1d0 -[PHImageRequest startRequest] + 2424
16  libdispatch.dylib             	       0x19eee5aac _dispatch_call_block_and_release + 32
17  libdispatch.dylib             	       0x19eeff584 _dispatch_client_callout + 16
18  libdispatch.dylib             	       0x19eeee2d0 _dispatch_lane_serial_drain + 740
19  libdispatch.dylib             	       0x19eeeede0 _dispatch_lane_invoke + 440
20  libdispatch.dylib             	       0x19eef91dc _dispatch_root_queue_drain_deferred_wlh + 292
21  libdispatch.dylib             	       0x19eef8a60 _dispatch_workloop_worker_thread + 540
22  libsystem_pthread.dylib       	       0x2214d5660 _pthread_wqthread + 292
23  libsystem_pthread.dylib       	       0x2214d29f8 start_wqthread + 8

I noticed that this crash is only happening when we are trying to request an asset which was clicked using Apple native camera.

Can you provide a short code listing showing how you are using these APIs when this crash occurs? I'd like to try to reproduce your results here.

The app crashes when attempting to fetch the latest image from the gallery. The crash occurs specifically when the latest image is a freshly captured photo from the native Camera app.

var allPhotosOptions: PHFetchOptions {
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        fetchOptions.predicate = NSPredicate(format: "mediaType = \(PHAssetMediaType.image.rawValue)")
        return fetchOptions
    }

    func requestImageFromPhotoLibrary() {
        var selectedAssets = [String]()
        let allPhotos = PHAsset.fetchAssets(with: allPhotosOptions)
        let firstAsset = allPhotos.firstObject?.localIdentifier
        selectedAssets.append(firstAsset ?? "")
        let assetResult = PHAsset.fetchAssets(withLocalIdentifiers: selectedAssets, options: nil)
        // Perform assets creation in background thread
        DispatchQueue.global(qos: .background).async { [weak self] in
            if let phAsset = assetResult.firstObject {
                let options = PHImageRequestOptions()
                options.isNetworkAccessAllowed = true
                options.deliveryMode = .highQualityFormat

                let resultHandler: (Data?, String?, CGImagePropertyOrientation,
                                    [AnyHashable: Any]?) -> Void = { [weak self] (data, _, _, _) in
                    guard let self = self else { return }
                    if let imageData = data, let assetImage = UIImage(data: imageData) {
                        print("Image size: \(assetImage.size)")
                    } else {
                        print("Didn't get assets from PhotosFramework")
                    }
                }
                PHImageManager.default().requestImageDataAndOrientation(for: phAsset, options: options,
                                                                        resultHandler: resultHandler)
            }
        }
    }

Hi there. Thanks for providing some source. I tried running this on an iPhone 16 running 18.3.2 immediately after taking a photo with the iPhone's camera and it did not crash. I tried a couple of times. I pasted this code into the ViewController of a new Xcode project and I called your requestImageFromPhotoLibrary() from viewDidLoad(). Is there anything else I need to do to make it crash?

Hi, the issue on our app is intermittent and it's happening with the above code snippet. I tried implementing the same with sample app myself and it was not happening.

So is there any build settings or any other settings which might be causing this?

That sounds like it could be something else in your app causing a problem. I recommend doing some additional debugging to try to figure out what's going on there. Maybe even making a copy of your app and trying to identify what's causing the error using a process of elimination by turning different parts off.

[iOS 18.0] PHImageManager request image crash
 
 
Q