我正在尋找我的應用程序中的圖像「保存」按鈕。問題是,iPhone 5和iPhone 4的圖像模糊。這是我現在使用的代碼。如何以編程方式爲iPhone5截圖?
爲iPhone 4和4S的用戶及其:
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[self.view.layer renderInContext:ctx];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], CGRectMake (0, 20, self.view.bounds.size.width, (self.view.bounds.size.height - 40)));
//save photo to photoAlbum
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Saved!"
message:@"Image has been Saved, don't forget to Share!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
UIImage *image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
// Request to save the image to camera roll
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil); }
對於iPhone 5的用戶,我有:
if(result.height == 568)
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[self.view.layer renderInContext:ctx];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], CGRectMake (0, 40, self.view.bounds.size.width, (self.view.bounds.size.height - 150)));
//save photo to photoAlbum
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Saved!"
message:@"Image has been Saved, don't forget to Share!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
UIImage *image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
// Request to save the image to camera roll
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil); }
}