1.将一个指定的图形放大或缩小为指定的尺寸,可以试试以下代码
-(UIImage*)scaleToSize:(UIImage*)img size:(CGSize)size
{
// 创建一个bitmap的context
// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(size);
// 绘制改变大小的图片
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
// 返回新的改变大小后的图片
return scaledImage;
}
2.图片如何存入 iPhone 本地 Documents 的方法
在 UIButton 的 saveImage 方法里添加一张图片,以下代码可将图片存入本地的 Documents 下:
-(void)saveImage
{
NSANSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
//also be .jpg or another
UIImage *image = imageView.image; // imageView is your image
// Returns the data for the specified image in JPEG/PNG format.
NSData *imageData = UIImagePNGRepresentation(image); //UIImageJPEGRepresentation(image)
[imageData writeToFile:savedImagePath atomically:NO];
}
3. UIImageWriteToSavedPhotosAlbum保存图片的方法
用UIImageWriteToSavedPhotosAlbum往照片库里面存图片时,经常发生缩略图能看到但原图消失的问题
用 UIImageWriteToSavedPhotosAlbum(imageSave, nil, nil, nil), imageSave是UIImage类型,这样就保存进去了。
而且注意图片不宜过大,以免程序崩溃
4.保存已选取的图片,下次启动App时直接显示的方
如果您想在iPhone App里实现保存一张图片,下次再启动该App时直接自动显示该图的功能,可以使用以下方法
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
[NSData writeToFile: imagePath atomically:YES];
保存起来,启动时再读出就OK了
UIImage *_image = [[UIImage alloc]initWithContentsOfFile: imagePath];
5.在以图片为背景的view上直接写文字
-(void)drawAtPoint:(CGPoint)point withFont:(UIFont*)font orientation:(WBOrientation)orient;
-(CGPoint)drawInRect:(CGRect)rect withFont:(UIFont*)font orientation:(WBOrientation)orient alignment:(UITextAlignment)align;
UIFont * font = [UIFont fontWithName:@"Arial" size:18];
NSString *str = @"Hello World";
[str drawAtPoint:CGPointMake(0,70) withFont:font];
6.从 iPhone/iPad 图片库中读取图片的代码
如果您的App涉及到从iPhone/iPad图片库读取图片,不妨看看CocoaChina版主“angellixf”分享的代码,会为您节省很多时间。
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
UIImagePickerControllerSourceTypePhotoLibrary,// 相片库
UIImagePickerControllerSourceTypeCamera//相机获取图片
UIImagePickerControllerSourceTypeSavedPhotosAlbum// 这个是自定义库,是由用户截图或保存到里面的
将图片保存到相片库的代码:
UIImageWriteToSavedPhotosAlbum(Image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
图片圆角
static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,
float ovalHeight)
{
float fw, fh;
if (ovalWidth == 0 || ovalHeight == 0) {
CGContextAddRect(context, rect);
return;
}
CGContextSaveGState(context);
CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGContextScaleCTM(context, ovalWidth, ovalHeight);
fw = CGRectGetWidth(rect) / ovalWidth;
fh = CGRectGetHeight(rect) / ovalHeight;
CGContextMoveToPoint(context, fw, fh/2); // Start at lower right corner
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); // Top right corner
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right
CGContextClosePath(context);
CGContextRestoreGState(context);
}
+ (id) createRoundedRectImage:(UIImage*)image size:(CGSize)size
{
// the size of CGContextRef
int w = size.width;
int h = size.height;
UIImage *img = image;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGRect rect = CGRectMake(0, 0, w, h);
CGContextBeginPath(context);
addRoundedRectToPath(context, rect, 10, 10);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
直接调用createRoundedRectImage....
返回圆角图片
圆角大小自行修改CGContextAddArcToPoint....
转帖 : yu