欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Objective C从远程url下载图片方法汇总

程序员文章站 2022-03-02 15:09:49
objective c从远程url下载图片 - (uiimage *) getimagefromurl: (nsstring *)theurl { ui...

objective c从远程url下载图片

- (uiimage *) getimagefromurl: (nsstring *)theurl {
  uiimage *theimage = null;
  nsstring *imagefilename = [bt_strings getfilenamefromurl:theurl];
  nsdata *imagedata = [[nsdata alloc] initwithcontentsofurl:[nsurl urlwithstring:theurl]];
  theimage = [[uiimage alloc] initwithdata:imagedata];
  [bt_filemanager saveimagetofile:theimage filename:imagefilename];
  return theimage;
}

objective c从远程地址获取图片并修改尺寸

nsstring* imageurl = [nsstring stringwithformat: @"http://theimageurl.com/?id=%@", [[resultsentries objectatindex:0] objectforkey: @"image_large"]];
nsdata* imagedata = [[nsdata alloc]initwithcontentsofurl:[nsurl urlwithstring:imageurl]]; 
uiimage* image = [[uiimage alloc] initwithdata:imagedata];
 
// resize image
cgsize newsize = cgsizemake(100, 100);
uigraphicsbeginimagecontext( newsize );// a cgsize that has the size you want
[image drawinrect:cgrectmake(0,0,newsize.width,newsize.height)];
 
//image is the original uiimage
uiimage* newimage = uigraphicsgetimagefromcurrentimagecontext();
uigraphicsendimagecontext();    
 
imageheight = image.size.height;
[imagemain setimage:newimage];
[imagedata release];
[image release];

以上所述就是本文的全部内容了,希望大家能够喜欢。