Muh*_*war 3 objective-c afnetworking sdwebimage
如何从objective-C中的URL获取图像的大小(高度/宽度)?我希望我的容器大小根据图像.我正在AFNetworking 3.0.
使用,SDWebImage如果它满足我的要求我可以使用.
Ale*_*lex 12
在许多情况下,在实际加载图像之前知道图像的大小可能是必要的.例如,在稍后在cellForRowAtIndexPath中加载实际图像时,在heightForRowAtIndexPath方法中设置tableView单元格的高度(这是非常频繁的捕获22).
一种简单的方法是使用Image I/O接口从服务器URL读取映像头:
#import <ImageIO/ImageIO.h>
NSMutableString *imageURL = [NSMutableString stringWithFormat:@"http://www.myimageurl.com/image.png"];
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)[NSURL URLWithString:imageURL], NULL);
NSDictionary* imageHeader = (__bridge NSDictionary*) CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
NSLog(@"Image header %@",imageHeader);
NSLog(@"PixelHeight %@",[imageHeader objectForKey:@"PixelHeight"]);
Run Code Online (Sandbox Code Playgroud)
斯威夫特 4.x
Xcode 12.x
func sizeOfImageAt(url: URL) -> CGSize? {
// with CGImageSource we avoid loading the whole image into memory
guard let source = CGImageSourceCreateWithURL(url as CFURL, nil) else {
return nil
}
let propertiesOptions = [kCGImageSourceShouldCache: false] as CFDictionary
guard let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, propertiesOptions) as? [CFString: Any] else {
return nil
}
if let width = properties[kCGImagePropertyPixelWidth] as? CGFloat,
let height = properties[kCGImagePropertyPixelHeight] as? CGFloat {
return CGSize(width: width, height: height)
} else {
return nil
}
}
Run Code Online (Sandbox Code Playgroud)
你可以这样尝试:
NSData *data = [[NSData alloc]initWithContentsOfURL:URL];
UIImage *image = [[UIImage alloc]initWithData:data];
CGFloat height = image.size.height;
CGFloat width = image.size.width;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5614 次 |
| 最近记录: |