DefaultRepresentation fullScreenImage on ALAsset does not return the image in full screen

In my application, I save images to an album as assets. I also want to get them and display them in full screen. I am using the following code:

ALAsset *lastPicture = [scrollArray objectAtIndex:iAsset]; ALAssetRepresentation *defaultRep = [lastPicture defaultRepresentation]; UIImage *image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] scale:[defaultRep scale] orientation: (UIImageOrientation)[defaultRep orientation]]; 

The problem is that the returned image is nil . I read the ALAssetRepresentation link that when the image does not fit, it returns zero.

I put this image in a UIImageView , the size of which is the size of the iPad screen. I was wondering, can you help me in this matter?

Thanks in advance.

+6
source share
1 answer

I am not a fan of fullScreenImage or fullResolutionImage. I found that when you do this across multiple assets in a queue, even if you immediately release UIImage, memory usage will increase dramatically, and that shouldn't. Also, when using fullScreenImage or fullResolutionImage, the returned UIImage is still compressed, which means that it will be unpacked before being drawn for the first time, thus in the main thread, which will block your user interface.

I prefer to use this method.

 -(UIImage *)fullSizeImageForAssetRepresentation:(ALAssetRepresentation *)assetRepresentation { UIImage *result = nil; NSData *data = nil; uint8_t *buffer = (uint8_t *)malloc(sizeof(uint8_t)*[assetRepresentation size]); if (buffer != NULL) { NSError *error = nil; NSUInteger bytesRead = [assetRepresentation getBytes:buffer fromOffset:0 length:[assetRepresentation size] error:&error]; data = [NSData dataWithBytes:buffer length:bytesRead]; free(buffer); } if ([data length]) { CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef)data, nil); NSMutableDictionary *options = [NSMutableDictionary dictionary]; [options setObject:(id)kCFBooleanTrue forKey:(id)kCGImageSourceShouldAllowFloat]; [options setObject:(id)kCFBooleanTrue forKey:(id)kCGImageSourceCreateThumbnailFromImageAlways]; [options setObject:(id)[NSNumber numberWithFloat:640.0f] forKey:(id)kCGImageSourceThumbnailMaxPixelSize]; //[options setObject:(id)kCFBooleanTrue forKey:(id)kCGImageSourceCreateThumbnailWithTransform]; CGImageRef imageRef = CGImageSourceCreateThumbnailAtIndex(sourceRef, 0, (__bridge CFDictionaryRef)options); if (imageRef) { result = [UIImage imageWithCGImage:imageRef scale:[assetRepresentation scale] orientation:(UIImageOrientation)[assetRepresentation orientation]]; CGImageRelease(imageRef); } if (sourceRef) CFRelease(sourceRef); } return result; } 

You can use it as follows:

 // Get the full image in a background thread dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ UIImage* image = [self fullSizeImageForAssetRepresentation:asset.defaultRepresentation]; dispatch_async(dispatch_get_main_queue(), ^{ // Do something with the UIImage }); }); 
+8
source

Source: https://habr.com/ru/post/920012/


All Articles