我正在尝试在我的项目中使用MosaicUI项目,
我面临的问题是如何从加载本地图像从 json 文件转移到我的网络服务器中的图像?
有没有人尝试过这个可能是?谢谢。
您可以使用 AFNetWorking 从网站加载图像。像这样更改 MosaicDataView.m 中的方法 setModule
-(void)setModule:(MosaicData *)newModule
{
module = newModule;
__weak UIImageView *weakImage = imageView;
__weak UIView *weakView = self;
__weak MosaicData *weakModule = module;
UIImage *image = [imageView loadImage:module.tg_id ofType:@"png"];
if (image)
{
[imageView setImage:image];
}
else
{
[imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:module.l_cover_url]] placeholderImage:[UIImage imageNamed:@"test.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
[weakImage setImage:image];
CGSize imgFinalSize = CGSizeZero;
[weakImage saveImage:image withFileName:weakModule.tg_id ofType:@"png"];
if (image.size.width < image.size.height)
{
imgFinalSize.width = weakView.bounds.size.width;
imgFinalSize.height = weakView.bounds.size.width * image.size.height / image.size.width;
// This is to avoid black bars on the bottom and top of the image
// Happens when images have its height lesser than its bounds
if (imgFinalSize.height < weakView.bounds.size.height)
{
imgFinalSize.width = weakView.bounds.size.height * weakView.bounds.size.width / imgFinalSize.height;
imgFinalSize.height = weakView.bounds.size.height;
}
}
else
{
imgFinalSize.height = weakView.bounds.size.height;
imgFinalSize.width = weakView.bounds.size.height * image.size.width / image.size.height;
// This is to avoid black bars on the left and right of the image
// Happens when images have its width lesser than its bounds
if (imgFinalSize.width < weakView.bounds.size.width)
{
imgFinalSize.height = weakView.bounds.size.height * weakView.bounds.size.width / imgFinalSize.height;
imgFinalSize.width = weakView.bounds.size.width;
}
}
weakImage.frame = CGRectMake(0, 0, imgFinalSize.width, imgFinalSize.height);
weakImage.center = CGPointMake(weakView.frame.size.width/2, weakView.frame.size.height/2);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
{
NSLog(@"%@",error);
}];
}
// Set new title
NSInteger marginLeft = self.frame.size.width / 20;
NSInteger marginBottom = self.frame.size.height / 20;
titleLabel.text = module.title;
titleLabel.font = [self fontWithModuleSize];
CGSize newSize = [module.title sizeWithFont:titleLabel.font constrainedToSize:titleLabel.frame.size];
CGRect newRect = CGRectMake(10+marginLeft,
self.frame.size.height - newSize.height - marginBottom,
newSize.width-30,
newSize.height);
titleLabel.frame = newRect;
}