2014-01-31 2 views
0

로드하지 않고 이미지의 속성을 읽거나 다운로드해야합니다. 사실 나는 이것을 달성하기 위해 CGImageSourceCreateWithUrl을 사용하는 간단한 메소드를 구현했다. 내 문제는 imageSource가 null 인 것처럼 보이기 때문에 항상 오류가 반환된다는 것입니다. 그래서 그것을 고칠 수 있습니까? NSURL 객체에서 다음과 같은 URL을 전달합니다. "http://www.example.com/wp-content/uploads/image.jpg"하지만 ALAsset 라이브러리 ID는 "assets-library : //asset/asset.JPG? id = E5F41458-962D-47DD-B5EF- E606E2A8AC7A & ext = JPG ".CGImageSourceCreateWithURL은 항상 NULL을 반환합니다.

에서 : 시도가 그것을 해결하기 위해 아무것도 작동 보인다 내가이 글을 본 한

-(NSString *) getPhotoInfo:(NSString *)paths{ 
    NSString *xmlList = @“test”; 

    NSURL * imageFileURL = [NSURL fileURLWithPath:paths]; 
    NSLog(@"imageFileURL %@", imageFileURL); 
    CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)(imageFileURL), NULL); 
    if (imageSource == NULL) { 
    // Error loading image 
    NSLog(@"Error loading image"); 
    } 
    CGFloat width = 0.0f, height = 0.0f; 
    CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL); 
    NSLog(@"image source %@", imageSource); 

    return xmlList; 
} 

: 이 내 방법입니다 내 프로젝트 ARC가 사용 설정되었습니다.

감사

답변

5

당신이 -fileURLWithPath에 문자열 "http://www.example.com/wp-content/uploads/image.jpg"를 전달하는 경우. 해당 문자열이 아닌 파일 경로, 그것은 URL 문자열 확신이기 때문에이 전무를 반환 것의

생각해을 - fileURLWithPath : "file : // localhost /"...와 같이 전달되는 문자열을 앞에 붙이면 "file : // localhost/http://www.example.com/wp-content/uploads/image.jpg"과 같은 URL로 끝납니다. 그 좋지 않다.

파일 시스템 경로 문자열뿐만 아니라 전체 URL 문자열을 전달하려면 [NSURL URLWithString : paths]를 호출해야합니다. ? 자산 라이브러리 "작업

+0

감사에 대한 설명은이 코드를 시도 그것은 더욱 분명하다 "http://example.com/name.jpg"와 같은 URL을 사용하는 경우에는 "assets-library : //asset/asset.JPG? id = E5F41458- 962D-47DD-B5EF-E606E2A8AC7A & ext = JPG "이것은 ALAsset 라이브러리를 사용하여 생성됩니다.이 경우 항상 null을 반환합니다. – Hieicker

+0

[NSURL URLWithString :]은"assets-library : //asset/asset.JPG? id = E5F41458-962D-47DD-B5EF-E606E2A8AC7A & ext = JP G "라고 생각합니다. 그것은 nil을 반환합니다. –

+0

감사! 사실 실제로 실제로는 반환하지 않습니다. 그러나 이런 종류의 오류 "ImageIO : CGImageSourceCreateWithURL CFURLCreateDataAndPropertiesFromResource가 오류 코드 -11로 실패했습니다." 또한 "Error loading image"오류를 반환하고 분명히 반환합니다 (항상 로그에 있음). imageSource (null) – Hieicker

1

:. //asset/asset.JPG ID가 ..........,

-(UIImage*)resizeImageToMaxSize:(CGFloat)max anImage:(UIImage*)anImage 
{ 

    NSData * imgData = UIImageJPEGRepresentation(anImage, 1); 
CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imgData, NULL); 
if (!imageSource) 
    return nil; 

CFDictionaryRef options = (CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys: 
              (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform, 
              (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent, 
              (id)[NSNumber numberWithFloat:max], (id)kCGImageSourceThumbnailMaxPixelSize, 
              nil]; 
CGImageRef imgRef = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options); 

UIImage* scaled = [UIImage imageWithCGImage:imgRef]; 
    //these lines might be skipped for ARC enabled 
CGImageRelease(imgRef); 
CFRelease(imageSource); 

return scaled; } 
관련 문제