图像不显示在iOS应用程序
问题描述:
我试图在URL中显示带有“https”的图像,但我不知道它为什么不显示。 (例如https://www.starkmedia.com/blog/wp-content/uploads/2016/02/https.png)。我不知道发生了什么问题,因为我也没有得到任何例外。有人能帮我弄清楚什么地方出了问题,怎样才能正确显示图像?图像不显示在iOS应用程序
我是Xcode的新手,我只是想对其他开发人员编写的代码进行一些更改。
- (void)downloadImageFromUrl:(NSString *)urlString
{
NSLog(@"Url String: %@", urlString);
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 0.0, 179, 245)];
NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:urlString]];
self.imageView.image = [UIImage imageWithData:imageData];
self.imageView.userInteractionEnabled = YES;
self.imageView.autoresizesSubviews = YES;
[MBProgressHUD hideHUDForView:self.view animated:YES];
[self showImage];
}
- (void)showImage
{
if ([self.scrollView.subviews count] == 3) {
[self.imageView removeFromSuperview];
}
self.scrollView.alpha = 1;
self.imageView = [[UIImageView alloc] initWithImage:self.image];
self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=self.image.size};
[self.scrollView addSubview:self.imageView];
// 2
self.scrollView.contentSize = self.image.size;
// 3
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(scrollViewDoubleTapped:)];
doubleTapRecognizer.numberOfTapsRequired = 2;
doubleTapRecognizer.numberOfTouchesRequired = 1;
[self.scrollView addGestureRecognizer:doubleTapRecognizer];
UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(scrollViewTwoFingerTapped:)];
twoFingerTapRecognizer.numberOfTapsRequired = 1;
twoFingerTapRecognizer.numberOfTouchesRequired = 2;
[self.scrollView addGestureRecognizer:twoFingerTapRecognizer];
// 4
CGRect scrollViewFrame = self.scrollView.frame;
CGFloat scaleWidth = scrollViewFrame.size.width/self.scrollView.contentSize.width;
CGFloat scaleHeight = scrollViewFrame.size.height/self.scrollView.contentSize.height;
CGFloat minScale = MIN(scaleWidth, scaleHeight);
self.scrollView.minimumZoomScale = minScale;
// 5
NSString *model = [[UIDevice currentDevice] model];
if([model isEqualToString:@"iPad"]) {
self.scrollView.maximumZoomScale = 1.5f;
}
else {
self.scrollView.maximumZoomScale = 1.5f;
}
self.scrollView.zoomScale = minScale;
// 6
[self centerScrollViewContents];
}
答
在两个办法做到这一点添加手动或使用波德
手动
1.增加SDWebImage文件夹到你的应用程序。
- ,并确保您要添加到所有要使用该库的目标。
3.Now导入这在你的头文件。
#import <SDWebImage/UIImageView+WebCache.h>
#import "UIImageView+WebCache.h"
4.使用下面的代码
[self.imageView sd_setImageWithURL:[NSURL URLWithString:"yoururl.png"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
5.Done。
使用POD
这将帮助您查看何时您的依赖项获得新更新。你也会知道你使用的是哪个版本。这是一个很好的做法。
1.你可以使用Cocoapods。转到您的终端,输入:
$ sudo gem install cocoapods
2.然后去你项目文件夹(地方,在这里你有xcodeproj)和类型:
$ pod init
3,本创建一个名为Podfile文件。打开它,并粘贴:
platform :ios, '8.0' // or whatever you need
use_frameworks!
pod 'SDWebImage', '~> 3.7'
4.So当你拥有它准备好了,打开终端和类型:
$ pod install
5.From现在你应该对xcworkspace代替OD xcodeproj工作。
6.现在在您的头文件中导入。
#import <SDWebImage/UIImageView+WebCache.h>
#import "UIImageView+WebCache.h"
4.使用下面的代码
[self.imageView sd_setImageWithURL:[NSURL URLWithString:"yoururl.png"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
5.Done。
希望这会有所帮助。