iphone ios 如何创建缩率图

以下大家友情支持一下:

做了一个产品,需要人气支持一下,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 image
UIGraphicsBeginImageContext(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 context
CGContextTranslateCTM(currentContext, -((mainImage.size.width – sideFull) / 2) * scaleFactor, 0);
}
else {
//a portfolio image – make context shift the original image upwards when drawn into the context
CGContextTranslateCTM(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 code
CGContextScaleCTM(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 image
UIGraphicsBeginImageContext(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 context
UIImage *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 image
NSData    *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 如何创建缩率图)