iphone ios 如何创建缩率图
来源:互联网 发布:建立数据库的表结构 编辑:程序博客网 时间:2024/06/05 15:26
以下大家友情支持一下:
做了一个产品,需要人气支持一下,android和iphone上91市场搜索#super junior粉丝团#,或者直接到页面下载http://m.ixingji.com/m.html?p=X16,大家帮忙捧捧场。
两篇博文
1。How to create square thumbnails using iPhone SDK / CG Quartz 2D
http://www.nickkuh.com/iphone/how-to-create-square-thumbnails-using-iphone-sdk-cg-quartz-2d/2010/03/
2。iPhone: Creating an image thumbnail
http://johnnytrops.com/blog/wp/2009/02/03/iphone-creating-an-image-thumbnail/
3.Resize a UIImage the right way
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
4。如何获取视频的缩率图
http://stackoverflow.com/questions/10209012/grabbing-first-frame-of-a-video-thumbnail-resolution-iphone
1.方法一函数
- (UIImage *)thumbWithSideOfLength:(float)length {NSString *subdir = @”my/images/directory”;NSString *filename = @”myOriginalImage.png”;NSString *fullPathToThumbImage = [subdir stringByAppendingPathComponent:[NSString stringWithFormat:@"%dx%d%@",(int) length, (int) length,filename];NSString *fullPathToMainImage = [subdir stringByAppendingPathComponent:filename];UIImage *thumbnail;NSFileManager *fileManager = [NSFileManager defaultManager];if ([fileManager fileExistsAtPath:fullPathToThumbImage] == YES) {thumbnail = [UIImage imageWithContentsOfFile:fullPathToThumbImage];}else {//couldn’t find a previously created thumb image so create one first…UIImage *mainImage = [UIImage imageWithContentsOfFile:fullPathToMainImage];UIImageView *mainImageView = [[UIImageView alloc] initWithImage:mainImage];BOOL widthGreaterThanHeight = (mainImage.size.width > mainImage.size.height);float sideFull = (widthGreaterThanHeight) ? mainImage.size.height : mainImage.size.width;CGRect clippedRect = CGRectMake(0, 0, sideFull, sideFull);//creating a square context the size of the final image which we will then// manipulate and transform before drawing in the original imageUIGraphicsBeginImageContext(CGSizeMake(length, length));CGContextRef currentContext = UIGraphicsGetCurrentContext();CGContextClipToRect( currentContext, clippedRect);CGFloat scaleFactor = length/sideFull;if (widthGreaterThanHeight) {//a landscape image – make context shift the original image to the left when drawn into the contextCGContextTranslateCTM(currentContext, -((mainImage.size.width – sideFull) / 2) * scaleFactor, 0);}else {//a portfolio image – make context shift the original image upwards when drawn into the contextCGContextTranslateCTM(currentContext, 0, -((mainImage.size.height – sideFull) / 2) * scaleFactor);}//this will automatically scale any CGImage down/up to the required thumbnail side (length) when the CGImage gets drawn into the context on the next line of codeCGContextScaleCTM(currentContext, scaleFactor, scaleFactor);[mainImageView.layer renderInContext:currentContext];thumbnail = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();NSData *imageData = UIImagePNGRepresentation(thumbnail);[imageData writeToFile:fullPathToThumbImage atomically:YES];thumbnail = [UIImage imageWithContentsOfFile:fullPathToThumbImage];}return thumbnail;}
方法2函数
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{ // set the image we have chosen to the background of the current view. (OPTIONAL!)imageView.image = image; // write the file to the TEMP Directory in the iphone, then when the user has completed// selecting their image from the picker, we want to write both the full sized file, and the// thumbnail to the /tmp directory for use later (whatever you want) NSData *pngImage = UIImagePNGRepresentation(image);if([pngImage writeToFile:[NSString stringWithFormat:@"%@/tempImage.png", TEMP_FOLDER] atomically:YES]){ // now that we have saved our image, lets generate a thumbnail from that image, and// save that one as well. // lets essentially make a copy of the selected image.UIImage *myThumbNail = [[UIImage alloc] initWithData:pngImage]; // begin an image context that will essentially "hold" our new imageUIGraphicsBeginImageContext(CGSizeMake(60.0,60.0)); // now redraw our image in a smaller rectangle.[myThumbNail drawInRect:CGRectMake(0.0, 0.0, 60.0, 60.0)];//[myThumbNail release]; // make a "copy" of the image from the current contextUIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext(); // create a new view to place on our screen (OPTIONAL-- testing)UIImageView *thumbView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 60.0, 60.0)];thumbView.image = newImage;[self.view addSubview:thumbView]; // now grab the PNG representation of our imageNSData *thumbData = UIImagePNGRepresentation(newImage); // now finally we can write our new thumbnail to the tmp directory on the phone.if([thumbData writeToFile:[NSString stringWithFormat:@"%@/tempImageThumb.png", TEMP_FOLDER] atomically:YES]){NSLog(@"thumb written!");} // close the modal view[picker dismissModalViewControllerAnimated:YES]; }else{// there was an error writing the image file to the tmp folder....NSLog(@" there was an error writing the file to the temp directory");}}
- iphone ios 如何创建缩率图
- iphone ios 如何使用gcd
- iphone ios 如何使用gcd
- iphone ios 如何使用gcd
- iphone ios 如何使用gcd
- iphone ios 如何使用gcd
- iphone ios 如何使用gcd
- iphone ios 如何使用gcd
- iphone ios 如何使用gcd
- 【iOS开发】 创建第一款iPhone应用程序
- 如何创建一个iPhone Web应用
- iphone中如何创建一个定时器NSTimer
- 如何为Iphone应用创建启动界面
- iPhone IOS中DEB如何安装
- ios如何取得iphone/ipad的磁盘空间
- iphone ios 如何使用gcd,block
- iphone ios 如何使用gcd,block
- iphone ios 如何使用gcd,block
- 适配器模式 +代理模式
- 为什么编程语言以及数据库要从1970年1月1日开始计算时间
- Android Animation学习笔记
- HTML页面元素加载顺序研究报告
- WHY CQRS and EVENT SOURCING
- iphone ios 如何创建缩率图
- 程序员进阶之道—稳中求进
- android4.0.3下编译framework/base/policy
- Security vulnerability in MySQL/MariaDB sql/password.c
- 希尔排序问题
- js注入测试器
- centos 安装g++
- 黑马程序员_java基础知识学习总结一
- Console命令详解,让调试js代码变得更简单