Skip to content

Facing a Crash when loading Images (Large size < = 30mb, count <= 10) on a Collection View Cell #842

@ankit0812

Description

@ankit0812
typealias DownloadImageCompletionHandler = (Result<UIImage, NSError>) -> Void

extension UIImageView {
    func downloadAndSetImgForImageView(with url: URL?,
                                       placeholderImage: UIImage? = nil,
                                       targetSize: CGSize? = nil,
                                       renderingMode: UIImage.RenderingMode? = nil,
                                       completed: DownloadImageCompletionHandler? = nil) {
        guard let url = url else {
            self.image = placeholderImage
            completed?(.failure(NSError(domain: CommonLiterals.INVALID_URL, code: 400, userInfo: nil)))
            return
        }
        
        var request = ImageRequest(url: url)
        
        // Apply resizing processor if targetSize is provided
        if let targetSize = targetSize {
            request.processors = [ImageProcessors.Resize(size: targetSize, contentMode: .aspectFill)]
        }

        let options = ImageLoadingOptions(placeholder: placeholderImage,
                                          transition: .fadeIn(duration: 0.33))
        

        loadImage(with: request, options: options, into: self, completion: { result in
            switch result {
            case .success(let response):
                let finalImage = renderingMode != nil ? response.image.withRenderingMode(renderingMode!) : response.image
                self.image = finalImage
                completed?(.success(finalImage))
            case .failure(let error as NSError):
                self.image = nil
                completed?(.failure(error))
            }
        })
    }
    
    func downloadAndSetImageForImgView(with url: URL?,
                                       placeholderImage: UIImage? = nil,
                                       targetSize: CGSize? = nil,
                                       renderingMode: UIImage.RenderingMode? = nil,
                                       completed: DownloadImageCompletionHandler? = nil) {
        downloadAndSetImgForImageView(with: url, placeholderImage: placeholderImage, targetSize: targetSize, completed: completed)
    }
}

extension UIButton {
    func downloadAndSetImageForButton(with url: URL?,
                                      for state: UIControl.State = .normal,
                                      placeholderImage: UIImage? = nil,
                                      targetSize: CGSize? = nil,
                                      renderingMode: UIImage.RenderingMode? = nil,
                                      completed: DownloadImageCompletionHandler? = nil) {
        guard let url = url else {
            self.setImage(placeholderImage, for: state)
            completed?(.failure(NSError(domain: CommonLiterals.INVALID_URL, code: 400, userInfo: nil)))
            return
        }
        
        var request = ImageRequest(url: url)
        
        // Apply resizing processor if targetSize is provided
        if let targetSize = targetSize {
            request.processors = [ImageProcessors.Resize(size: targetSize, contentMode: .aspectFill)]
        }

        ImagePipeline.shared.loadImage(with: request) { result in
            switch result {
            case .success(let response):
                self.setImage(response.image, for: state)
                completed?(.success(response.image))
            case .failure(let error as NSError):
                self.setImage(placeholderImage, for: state)
                completed?(.failure(error))
            }
        }
    }
}

No Stacktrace :-(

Console Logs :

IOSurface creation failed: e00002c2 parentID: 00000000 properties: {
IOSurfaceAllocSize = 1800960000;
IOSurfaceBytesPerElement = 4;
IOSurfaceBytesPerRow = 120000;
IOSurfaceCacheMode = 0;
IOSurfaceHeight = 14993;
IOSurfaceMapCacheAttribute = 1;
IOSurfaceName = CMPhoto;
IOSurfaceOffset = 0;
IOSurfacePixelFormat = 1111970369;
IOSurfacePlaneExtendedPixelsOnBottom = 15;
IOSurfacePreallocPages = 0;
IOSurfaceWidth = 30000;
}
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceCacheMode
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceOffset
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceAllocSize
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceMapCacheAttribute
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceBytesPerRow
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceBytesPerElement
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceWidth
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfacePixelFormat
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfacePreallocPages
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfacePlaneExtendedPixelsOnBottom
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceName
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceHeight
IOSurface creation failed: e00002c2 parentID: 00000000 properties: {
IOSurfaceAllocSize = 1799160000;
IOSurfaceBytesPerElement = 4;
IOSurfaceBytesPerRow = 120000;
IOSurfaceCacheMode = 0;
IOSurfaceHeight = 14993;
IOSurfaceMapCacheAttribute = 1;
IOSurfaceName = CMPhoto;
IOSurfaceOffset = 0;
IOSurfacePixelFormat = 1111970369;
IOSurfacePreallocPages = 0;
IOSurfaceWidth = 30000;
}
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceCacheMode
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceWidth
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfacePixelFormat
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceMapCacheAttribute
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceHeight
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceAllocSize
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceBytesPerRow
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceBytesPerElement
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfacePreallocPages
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceName
IOSurface creation failed: e00002c2 parentID: 00000000 property: IOSurfaceOffset

NOTE: targetSize is nil for the current use case. So does not deals with preprocessor.

cc: @kean

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions