您的位置:首页 > 其它

iphone 图像处理小结

2011-12-05 19:08 225 查看
1.本文摘自:http://blog.csdn.net/mkhgg/article/details/6585402

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: