如何从UIImageView创建UIImage?

时间:2010-12-23 11:29:34

标签: iphone image uiimageview uiimage animated

我正在尝试在我的应用程序上实现一组动画。

NSArray *myImages = [NSArray arrayWithObjects:
    [UIImage imageNamed:@"myImage1.png"],
    [UIImage imageNamed:@"myImage2.png"],
    [UIImage imageNamed:@"myImage3.png"],
    [UIImage imageNamed:@"myImage4.gif"],
    nil];

UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:[self bounds]];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 0.25; // seconds
myAnimatedView.animationRepeatCount = 0; // 0 = loops forever
[myAnimatedView startAnimating];
[self addSubview:myAnimatedView];
[myAnimatedView release];

好的,但问题是我正在使用API​​函数在地图上放置标记。

该功能如下:

RMMarker *newMarker;
UIImage *blueMarkerImage = [UIImage imageNamed:@"marker.png"];
newMarker = [[RMMarker alloc] initWithUIImage:blueMarkerImage anchorPoint:CGPointMake(0.5, 1.0)];


[mapView.contents.markerManager addMarker:newMarker AtLatLong:position];
[newMarker release];

问题是“initWithUIImage:”仅适用于UIImage,而不适用于UIImageView。

那么,我该如何解决呢?

更新:我的新代码无效:

NSArray *myImages = [NSArray arrayWithObjects:                           
                         [UIImage imageNamed:@"marker0.png"],
                         [UIImage imageNamed:@"marker1.png"],
                         [UIImage imageNamed:@"marker2.png"],
                         [UIImage imageNamed:@"marker3.png"],
                         [UIImage imageNamed:@"marker4.png"],
                         nil];

    UIImageView *myAnimatedView = [UIImageView alloc];
    myAnimatedView.animationImages = myImages;
    myAnimatedView.animationDuration = 10; // seconds
    myAnimatedView.animationRepeatCount = 1; // 0 = loops forever
    [myAnimatedView startAnimating];
    RMMarker *newMarker = [[RMMarker alloc] initWithUIImage:[myAnimatedView image] anchorPoint:CGPointMake(0.5, 1.0)];
    [mapView.contents.markerManager addMarker:newMarker AtLatLong:markerPosition];

2 个答案:

答案 0 :(得分:0)

您可以尝试使用image中的UIImageView消息:

 newMarker = 
     [
          [RMMarker alloc] 
              initWithUIImage:[myAnimatedView image] 
              anchorPoint:CGPointMake(0.5, 1.0)
     ];

或者,如果您需要创建UIImageView,那么您可以尝试:

 UIImageView *newMakerImageView = 
     [[UIImageView alloc] initWithImage:
        [
          [RMMarker alloc] 
              initWithUIImage:yourImageHere
              anchorPoint:CGPointMake(0.5, 1.0)
        ]
     ];

答案 1 :(得分:0)

UIImageView *myAnimatedView = [UIImageView alloc] 

这可能只是一个复制/粘贴错误,但你忘了init。

UIImageView *myAnimatedView = [[[UIImageView alloc] initWithFrame:[self bounds]] autorelease]