gpt4 book ai didi

objective-c - 如何减小使用相机拍摄的快照的文件大小?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:10:22 28 4
gpt4 key购买 nike

我在传真类型的 iOS 应用程序中使用相机,在这种情况下,图像往往会变得很大,并导致传真网络服务明显延迟。我没有嫁给更好的图像质量。我的问题是如何降低分辨率/减小文件大小,使其成为较小文件大小的图像?

#import <UIKit/UIKit.h>

//Definition of the delegate's interface
@protocol SnapShotViewControllerDelegate

-(void)selectedFileName:(NSString*)filename;

@end




@interface SnapShotViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate> {

IBOutlet UIButton *button;
IBOutlet UIImageView *image;
UIImagePickerController *imgPicker;

}

- (IBAction)grabImage;
- (IBAction)useImage;

@property (nonatomic, retain) UIImagePickerController *imgPicker;
@property (nonatomic,retain)UIImageView *image;
@property (nonatomic,retain)UIButton *button;
@property (nonatomic, assign) id<SnapShotViewControllerDelegate> delegate;

@end




#import "SnapShotViewController.h"
#import "PDFImageConverter.h"


@implementation SnapShotViewController

@synthesize imgPicker, image, button;
@synthesize delegate=_delegate;


- (IBAction)grabImage{

[self presentModalViewController:self.imgPicker animated:YES];

}

- (IBAction)useImage{

if ([image image]==nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Image Found" message:@"Click 'Grab Image' to take a snapshot first" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}else {

//do something with file



[self.delegate selectedFileName: filename];

[self.navigationController popViewControllerAnimated:YES];
}


}



- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
[image setImage:img];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];


}




// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];

if (imgPicker == nil) {
self.imgPicker = [[[UIImagePickerController alloc] init] autorelease];
}

//self.imgPicker.allowsImageEditing = YES;
imgPicker.delegate =self;

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;

}else

self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}



- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;

self.image = nil;
self.button = nil;


}


- (void)dealloc {

[imgPicker release];
[image release];
[button release];

[super dealloc];
}


@end

最佳答案

我忘记了我在哪里看到这段代码(也许在 SO 上) 我从 this thread 得到这段代码在我从 iPhone 相机获取图像后,我在我的一个应用程序中使用它:

CGSize newImageSize = CGSizeMake(100, 133);
self.studentPic.image = student.pic = [StudentInfo imageWithImage:[info objectForKey:UIImagePickerControllerOriginalImage] scaledToSize:newImageSize];

+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
// Create a graphics image context
UIGraphicsBeginImageContext(newSize);

// Tell the old image to draw in this new context, with the desired
// new size
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

// Get the new image from the context
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

// End the context
UIGraphicsEndImageContext();

// Return the new image.
return newImage;
}

关于objective-c - 如何减小使用相机拍摄的快照的文件大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6644645/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com