iOS 그림 URL에 따라 사이즈 얻기
/**
url
@param URL url
@return CGSize
*/
+ (CGSize)getImageSizeWithURL:(id)URL;
/**
* url
*/
+ (CGSize)getImageSizeWithURL:(id)URL{
NSURL * url = nil;
if ([URL isKindOfClass:[NSURL class]]) {
url = URL;
}
if ([URL isKindOfClass:[NSString class]]) {
url = [NSURL URLWithString:URL];
}
if (!URL) {
return CGSizeZero;
}
CGImageSourceRef imageSourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
CGFloat width = 0, height = 0;
if (imageSourceRef) {
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef, 0, NULL);
if (imageProperties != NULL) {
CFNumberRef widthNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
// if (widthNumberRef != NULL) {
// CFNumberGetValue(widthNumberRef, kCFNumberFloat64Type, &width);
// }
// CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
// if (heightNumberRef != NULL) {
// CFNumberGetValue(heightNumberRef, kCFNumberFloat64Type, &height);
// }
// 64
#if defined(__LP64__) && __LP64__
if (widthNumberRef != NULL) {
CFNumberGetValue(widthNumberRef, kCFNumberFloat64Type, &width);
}
CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
if (heightNumberRef != NULL) {
CFNumberGetValue(heightNumberRef, kCFNumberFloat64Type, &height);
}
#else
if (widthNumberRef != NULL) {
CFNumberGetValue(widthNumberRef, kCFNumberFloat32Type, &width);
}
CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
if (heightNumberRef != NULL) {
CFNumberGetValue(heightNumberRef, kCFNumberFloat32Type, &height);
}
#endif
CFRelease(imageProperties);
}
CFRelease(imageSourceRef);
}
return CGSizeMake(width, height);
}
실천이 증명한 바와 같이 인터넷의 또 다른 방법은 실행할 수 없고 문제가 존재할 수 없다. 상기 그림url:http://xs3.op.xywy.com/club.xywy.com/doc/20181017/0fd2a51f9435a4.jpg
// url
-(CGSize)getImageSizeWithURL:(id)imageURL
{
NSURL* URL = nil;
if([imageURL isKindOfClass:[NSURL class]]){
URL = imageURL;
}
if([imageURL isKindOfClass:[NSString class]]){
URL = [NSURL URLWithString:imageURL];
}
if(URL == nil)
return CGSizeZero; // url CGSizeZero
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
NSString* pathExtendsion = [URL.pathExtension lowercaseString];
CGSize size = CGSizeZero;
if([pathExtendsion isEqualToString:@"png"]){
size = [self getPNGImageSizeWithRequest:request];
}
else if([pathExtendsion isEqual:@"gif"])
{
size = [self getGIFImageSizeWithRequest:request];
}
else{
size = [self getJPGImageSizeWithRequest:request];
}
if(CGSizeEqualToSize(CGSizeZero, size)) // ,
{
NSData* data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:URL] returningResponse:nil error:nil];
UIImage* image = [UIImage imageWithData:data];
if(image)
{
size = image.size;
}
}
return size;
}
// PNG
-(CGSize)getPNGImageSizeWithRequest:(NSMutableURLRequest*)request
{
[request setValue:@"bytes=16-23" forHTTPHeaderField:@"Range"];
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if(data.length == 8)
{
int w1 = 0, w2 = 0, w3 = 0, w4 = 0;
[data getBytes:&w1 range:NSMakeRange(0, 1)];
[data getBytes:&w2 range:NSMakeRange(1, 1)];
[data getBytes:&w3 range:NSMakeRange(2, 1)];
[data getBytes:&w4 range:NSMakeRange(3, 1)];
int w = (w1 << 24) + (w2 << 16) + (w3 << 8) + w4;
int h1 = 0, h2 = 0, h3 = 0, h4 = 0;
[data getBytes:&h1 range:NSMakeRange(4, 1)];
[data getBytes:&h2 range:NSMakeRange(5, 1)];
[data getBytes:&h3 range:NSMakeRange(6, 1)];
[data getBytes:&h4 range:NSMakeRange(7, 1)];
int h = (h1 << 24) + (h2 << 16) + (h3 << 8) + h4;
return CGSizeMake(w, h);
}
return CGSizeZero;
}
// gif
-(CGSize)getGIFImageSizeWithRequest:(NSMutableURLRequest*)request
{
[request setValue:@"bytes=6-9" forHTTPHeaderField:@"Range"];
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if(data.length == 4)
{
short w1 = 0, w2 = 0;
[data getBytes:&w1 range:NSMakeRange(0, 1)];
[data getBytes:&w2 range:NSMakeRange(1, 1)];
short w = w1 + (w2 << 8);
short h1 = 0, h2 = 0;
[data getBytes:&h1 range:NSMakeRange(2, 1)];
[data getBytes:&h2 range:NSMakeRange(3, 1)];
short h = h1 + (h2 << 8);
return CGSizeMake(w, h);
}
return CGSizeZero;
}
// jpg
-(CGSize)getJPGImageSizeWithRequest:(NSMutableURLRequest*)request
{
[request setValue:@"bytes=0-209" forHTTPHeaderField:@"Range"];
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if ([data length] <= 0x58) {
return CGSizeZero;
}
if ([data length] < 210) {// DQT
short w1 = 0, w2 = 0;
[data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
[data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
short w = (w1 << 8) + w2;
short h1 = 0, h2 = 0;
[data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
[data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
short h = (h1 << 8) + h2;
return CGSizeMake(w, h);
} else {
short word = 0x0;
[data getBytes:&word range:NSMakeRange(0x15, 0x1)];
if (word == 0xdb) {
[data getBytes:&word range:NSMakeRange(0x5a, 0x1)];
if (word == 0xdb) {// DQT
short w1 = 0, w2 = 0;
[data getBytes:&w1 range:NSMakeRange(0xa5, 0x1)];
[data getBytes:&w2 range:NSMakeRange(0xa6, 0x1)];
short w = (w1 << 8) + w2;
short h1 = 0, h2 = 0;
[data getBytes:&h1 range:NSMakeRange(0xa3, 0x1)];
[data getBytes:&h2 range:NSMakeRange(0xa4, 0x1)];
short h = (h1 << 8) + h2;
return CGSizeMake(w, h);
} else {// DQT
short w1 = 0, w2 = 0;
[data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
[data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
short w = (w1 << 8) + w2;
short h1 = 0, h2 = 0;
[data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
[data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
short h = (h1 << 8) + h2;
return CGSizeMake(w, h);
}
} else {
return CGSizeZero;
}
}
}
출처:https://www.jianshu.com/p/9984c37f3f54
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.