使用预先构建的系统来抓取远程图像并将其保存到服务器.
目前没有检查图像是否确实存在于该远程位置,并且它具有某种文件类型(jpg,jpeg,gif),并且我的任务是同时执行这两种操作.
我认为这非常简单,因为我只使用一个简单的正则表达式和getimagesize($ image):
$remoteImageURL = 'http://www.exampledomain.com/images/image.jpg';
if(@getimagesize($remoteImageURL) && preg_match("/.(jpg|gif|jpeg)$/", $remoteImageURL) )
{
// insert the image yadda yadda.
}
Run Code Online (Sandbox Code Playgroud)
当我对我正在抓取图像的URL没有任何控制权时会出现问题,例如:
http://www.exampledomain.com/images/2?num=1
所以当谈到这一点时,正则表达式和getimagesize()都会失败,有没有更好的方法呢?
我正在尝试构建一个示例应用程序,它从URL中抓取一些json并将其显示在表视图中.我在将数据存储到对象中并将这些对象插入NSMutableArray时遇到问题.
TableView.m
- (void) getDataFromAPI {
NSString *endpoint = @"http://www.somesite.com/list.php";
NSURL *url = [NSURL URLWithString:endpoint];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[self createMovies: JSON];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response , NSError *error , id JSON){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Uh oh, there's been a problem!" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Retry", nil];
[alert show];
}];
[operation start];
}
- (void) createMovies: (id) json {
NSMutableArray *list = [[NSMutableArray alloc] init]; …
Run Code Online (Sandbox Code Playgroud)