![](/img/trans.png)
[英]CGImageSourceRef imageSource = CGImageSourceCreateWithURL returns NULL
[英]CGImageSourceCreateWithURL returns always NULL
我需要讀取圖像的屬性而不進行加載或下載。 實際上,我已經實現了使用CGImageSourceCreateWithUrl的簡單方法來完成此任務。 我的問題是它總是返回錯誤,因為imageSource為null。 那我該怎么辦呢? 在NSURL對象中,我傳遞了URL,例如:“ http://www.example.com/wp-content/uploads/image.jpg ”,還傳遞了ALAssets庫ID,該ID用於檢索電話內的圖像,例如“ assets-library:// asset / asset.JPG?id = E5F41458-962D-47DD-B5EF-E606E2A8AC7A&ext = JPG”。 這是我的方法:
-(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。
謝謝
如果您將字符串“ http://www.example.com/wp-content/uploads/image.jpg ”傳遞給-fileURLWithPath:它將返回nil,因為該字符串肯定不是文件路徑,所以它是一個URL串。
想想-fileURLWithPath:就像在字符串前面加上“ file:// localhost /” ...這樣,您最終得到的URL類似於“ file:// localhost / http:// www。 example.com/wp-content/uploads/image.jpg ”。這不好。
如果要傳遞整個URL字符串,而不僅僅是文件系統路徑字符串,則需要調用[NSURL URLWithString:paths]。
使用“ assets-library://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; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.