开发者

UIImageView's on a UIView Layer, wrong frame output

开发者 https://www.devze.com 2023-03-31 11:03 出处:网络
I am trying to combine 2 UIImageViews and save them as 1. The top-most image is a static \"frame\", and the lower image is rotatable / scalable.

I am trying to combine 2 UIImageViews and save them as 1. The top-most image is a static "frame", and the lower image is rotatable / scalable.

My issue is that the photo needs to be saved as 640 x 960, however, the actual view that the 2 images sit on is 320 x 480 (so it shows correctly on the users screen). When these 2 images are combined, they are saved on a 640 x 960 view, however, the 2 images themselves are combined as 320 x 480 (as seen in the image example below).

Here is the code that I am currently using to get my wrong results.

CGSize deviceSpec;
if ( IDIOM == IPAD ) { deviceSpec =CGSizeMake(768,1024); } else { deviceSpec =CGSizeMake(640,960); }
UIGraphicsBeginImageContext( deviceSpec );

    UIView * rendered = [[UIView alloc] init];
    [[rendered layer] setFrame:CGRectMake(0,0,deviceSpec.width,deviceSpec.height)];
    [[rendered layer] addSublayer:[self.view layer]];
    [[rendered layer] renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * draft = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

AssetsLibrary

Should also mention I am saving the image using the following:

ALAssetsLibrary * library = [[[ALAssetsLibrary alloc] init] autorelease];
[library writeImageToSavedPhotosAlbum: draft.CGImage orientation:ALAssetOrientationUp 
                      completionBlock: ^(NSURL *assetURL, NSError *error)  
 {
     if (error) {
         NSLog(@"ALAssetLibrary error - %@", error);
     } else {
         NSLog(@"Image saved: %@", assetURL);
     }
 }];

The output

Note here, the entire white area is actually 640 x 960 and the 2 images are 320 x 480开发者_如何转开发.

UIImageView's on a UIView Layer, wrong frame output

Note: The actual image here is 640x960 (the entire area), where as the actual image (the photo) is 320x480 which is the actual size of the original layers frame.


To support different scales I used the following code to:

// If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen. 
UIGraphicsBeginImageContextWithOptions(compositeView.frame.size, compositeView.opaque, scale);
[compositeView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

So, you need to replace UIGraphicsBeginImageContext with UIGraphicsBeginImageContextWithOptions

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号