我有这段代码可用于从服务器下载图像:
- (void) loadDataWithOperation {
//Connection test
NSURL *url = [NSURL URLWithString:@"http://myurl.com/testconnection.php"];
NSError* error;
NSString* connected = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
//If the string Connected has NOT manged to initialise itself with the contents of the URL:
if (connected == NULL) {
//Display error picture:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Error downloading gallery, please check network connection and try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
[self createBackButton];
} else {
//Load Data
NSLog(@"Connected - %@",connected);
NSLog(@"loadDataWithOperartion");
//Create an array to hold the URLS
NSMutableArray *myURLS;
//Initialize the array with nil
myURLS = [[NSMutableArray alloc] init];
NSLog(@"Here1");
//Add all the URLs from the server to the array
for (int i = 0; i <= 4; i++){
NSString *tempString = [[NSString alloc] initWithFormat : @"http://myurl.com/GalleryImages/%djack1.jpg", i];
[myURLS addObject: [NSURL URLWithString:tempString]];
[tempString release];
}
//Array to hold the image data
NSMutableArray *myData;
//Initialize the array with nil
myData = [[NSMutableArray alloc] init];
NSLog(@"Here2");
//Add all the URLs from the server to the array
for (int i = 0; i <= 4; i++){
[myData addObject: [[NSData alloc] initWithContentsOfURL: [myURLS objectAtIndex:i]]];
}
//Array to hold the image data
NSMutableArray *myImages;
//Initialize the array with nil
myImages = [[NSMutableArray alloc] init];
NSLog(@"Here3");
//Add all the URLs from the server to the array
for (int i = 0; i <= 4; i++){
[myImages addObject: [UIImage imageWithData: [myData objectAtIndex:i]]];
}
// Load an array of images into the page view controller
//Initialising them with the data stored above
NSArray *array = [[NSArray alloc] initWithArray:myImages];
[self setImages:array];
//Release the image data
[myURLS release];
[myData release];
[myImages release];
[array release];
}
}
此代码有效,因为服务器上总是有一定数量的图像可供下载,在本例中为 4。
但是我的问题是,如果我要增加图像的数量,比如说 20,那么在图像下载然后显示时会等待很长时间。
基本上我想在加载屏幕上下载 5 张图片,然后下载剩余的 15 张图片,同时用户可以查看前 5 张图片。任何人都可以告诉我如何执行此操作吗?
当下载图片时,它们会缩放到 iPhone 屏幕的大小并放置在 UIScrollView 中以供查看。
谢谢,
jack
最佳答案
在后台下载图片:
-(void)imageDownloadStart
{
[self performSelectorInBackground:@selector(downloadImages) withObject:nil];
}
-(void)downloadImages
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
//WRITE YOU DOWNLOADING CODE HERE
if(yourCondition)
{
[self performSelectorOnMainThread:@selector(imageDownloadStart) withObject:nil waitUntilDone:YES];
}
[pool release];
}
并调整大小:
-(UIImage *)resizeImage:(UIImage *)image withSize:(CGSize)newSize
{
float actualHeight = image.size.height;
float actualWidth = image.size.width;
float imgRatio = actualWidth/actualHeight;
float maxRatio = newSize.width/newSize.height;
if(imgRatio!=maxRatio)
{
if(imgRatio < maxRatio){
imgRatio = newSize.width / actualHeight;
actualWidth = round(imgRatio * actualWidth);
actualHeight = newSize.width;
}
else{
imgRatio = newSize.height / actualWidth;
actualHeight = round(imgRatio * actualHeight);
actualWidth = newSize.height;
}
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[resizedImage release];
return resizedImage;
}
关于iphone - Objective - C 从网络下载图片问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6372062/
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
我是Rails的新手,所以请原谅简单的问题。我正在为一家公司创建一个网站。那家公司想在网站上展示它的客户。我想让客户自己管理这个。我正在为“客户”生成一个表格,我想要的三列是:公司名称、公司描述和Logo。对于名称,我使用的是name:string但不确定如何在脚本/生成脚手架终端命令中最好地创建描述列(因为我打算将其设置为文本区域)和图片。我怀疑描述(我想成为一个文本区域)应该仍然是描述:字符串,然后以实际形式进行调整。不确定如何处理图片字段。那么……说来话长:我在脚手架命令中输入什么来生成描述和图片列? 最佳答案 对于“文本”数
我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。